gpt4 book ai didi

java - 被覆盖或使用已弃用的 API?

转载 作者:行者123 更新时间:2023-12-01 18:34:53 26 4
gpt4 key购买 nike

这是我用来使用两个线程计算数字阶乘的代码。当我尝试编译它时,它说“....”使用或覆盖了已弃用的 API,请有人帮忙解决这个问题。

import java.lang.Thread;
import java.util.Scanner;

class A extends Thread
{
int n;
int fact=1;
int i;
A(int x)
{
n=x;
i=n;
}
public void run()
{
if(i>0)
{
fact=fact*i;
i--;
}
else
System.out.print(fact);
suspend();
}
}

class B extends Thread
{
int i;
int n;
int fact=1;
B(int x)
{
n=x;
i=n;
}
public void run()
{
if(i>0)
{
fact=fact*i;
i--;
}
else
System.out.print(fact);
suspend();
}
}

class refact
{
public static void main(String args[])
{
int n;
System.out.print("Enter the number you want :");
Scanner a = new Scanner(System.in);
n=a.nextInt();
System.out.print("\n\n");
A newthreadA = new A(n);
newthreadA.start();
B newthreadB = new B(n);
newthreadB.start();
}
}

此外,如果其他人有更好的想法来使用两个线程计算数字的阶乘,请提及。谢谢!

最佳答案

您收到错误是因为 Thread.suspend 已被弃用。以下是来自 docs 的更多信息:

Why are Thread.suspend and Thread.resume deprecated?

Thread.suspend is inherently deadlock-prone. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, deadlock results. Such deadlocks typically manifest themselves as "frozen" processes.

关于java - 被覆盖或使用已弃用的 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22447306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com