[英]Deadlock issue in threads
我有一个方法,使用数字来查找它是否是素数。 我已经制作了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();
}
}
}
几点注意事项:
您将lock
声明为实例变量,而不是类变量( static
)。 结果,每个线程都有自己的副本,这违背了目的。
你不需要做一个tryLock
然后sleep
- 只需调用.lock()
就会让线程进入睡眠状态,直到锁定可用。 或者,您可以使用AtomicInteger
完全绕过锁定: https : //docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html
(实际的错误)在你的素数检查器本身,你在1
开始for循环: n % 1
总是等于0,因为所有整数(根据定义)可以被1整除。唯一的原因2和3可能工作该limit
将等于1,因此for循环不会运行(正确的行为)。 你应该从2开始i
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.