Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线程中的死锁问题_Java_Multithreading_Locking_Deadlock - Fatal编程技术网

Java 线程中的死锁问题

Java 线程中的死锁问题,java,multithreading,locking,deadlock,Java,Multithreading,Locking,Deadlock,我有一个方法,它使用一个数字来确定它是否是素数。我已经做了10个线程,可以从计数器中寻找一个数字,如果它是素数,就打印出来。当我运行下面的代码时,程序中似乎出现了死锁,只打印了2和3。我是线程新手,所以我很确定我犯了一个愚蠢的错误。有人能帮我吗 public class Main{ public static int counter=0; public static void main(String[] args) { Thread thread[] = new Thread[10

我有一个方法,它使用一个数字来确定它是否是素数。我已经做了10个线程,可以从计数器中寻找一个数字,如果它是素数,就打印出来。当我运行下面的代码时,程序中似乎出现了死锁,只打印了2和3。我是线程新手,所以我很确定我犯了一个愚蠢的错误。有人能帮我吗

public class Main{

public static int counter=0;

public static void main(String[] args) {

    Thread thread[] = new Thread[10]; //10 threads created

    for(int i=0;i<10;i++){
        thread[i]=new Thread(new NewThread());
        thread[i].start();
    }

}

public static int increment(){

    long count=0;

    count=++counter;

    if(count>1000000000){
        return -1;
    }

    return counter;
}

}


public class NewThread implements Runnable{

    Lock lock=new ReentrantLock();

    public boolean isPrime(int number) throws ThresholdReachedException{

        if(number<0){
            throw new ThresholdReachedException("Limit Reached");
        }

        if(number==1){
            return false;
        }

        double limit=Math.pow(number, 0.5);

        for(int i=1;i<(int)limit;i++){
            if(number%i==0){
                return false;
            }
        }
        return true;
    }

    @SuppressWarnings("static-access")
    @Override
    public void run() {

        //System.out.println(Thread.currentThread().getName()+" running");
        int number=0;
        try{
            while(true){
                if(lock.tryLock()){
                //lock.lock();
                number=Main.increment();
                lock.unlock();
                }else{
                    Thread.currentThread().sleep(2000);;
                }

                if(isPrime(number)){
                    System.out.println(Thread.currentThread().getName()+" Prime number : "+number);
                }
            }
        }catch(ThresholdReachedException e){

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

    }

}
公共类主{
公共静态整数计数器=0;
公共静态void main(字符串[]args){
线程线程[]=新线程[10];//创建了10个线程
对于(整数i=0;I100000000){
返回-1;
}
返回计数器;
}
}
公共类NewThread实现Runnable{
Lock Lock=新的可重入锁();
公共布尔值isPrime(整数)抛出ThresholdReachedException{
如果(数字需要注意的事项很少:

  • 您正在将
    lock
    声明为实例变量,而不是类变量(
    static
    )。因此,每个线程都有自己的副本,这与此目的背道而驰

  • 您不需要先执行
    tryLock
    ,然后再执行
    sleep
    ——只需调用
    .lock()
    ,线程就会进入睡眠状态,直到锁可用。或者,您可以使用
    原子整数来完全绕过使用锁:

  • (实际的错误)在主检查器本身中,您在
    1
    处启动for循环:
    n%1
    始终等于0,因为(根据定义)所有整数都可以被1整除。2和3工作的唯一原因可能是
    limit
    将等于1,因此for循环不会运行(正确行为)。你应该改为从2开始


  • 这不是死锁,如果平方根至少为2,您可以通过验证一个数字不可除以1开始。将
    isPrime
    中的循环更改为从2开始,您应该可以。但是
    pow
    的精度可能仍然存在问题。我相信您应该使用非常感谢..我已将锁设置为静态的方法…并修改了素数方法…现在它可以工作了..谢谢你的欢迎:)如果答案有用,别忘了接受