为了账号安全,请及时绑定邮箱和手机立即绑定

以下程序为了学习而写,但是经常发生死锁,请问为什么?

以下程序为了学习而写,但是经常发生死锁,请问为什么?

沧海一幻觉 2022-10-01 14:10:01
两个线程依次按序打印从1到100,一个只打印奇数,另一个只打印偶数。public class Test16 implements Runnable{private int turn;private int num;private int sum;static Integer counter=0;public Test16(int turn,int num,int sum){this.turn=turn;this.num=num;this.sum=sum;}public  synchronized void run() {while(counter<sum){while(turn!=counter%num){try {wait();} catch (InterruptedException e) {e.printStackTrace();}}counter++;System.out.print(counter+" ");notifyAll();}}public static void main(String[] args){new Thread(new Test16(0,2,100)).start();new Thread(new Test16(1,2,100)).start();}}
查看完整描述

3 回答

?
慕虎7371278

TA贡献1802条经验 获得超4个赞

问题在public  synchronized void run() {  相当于 synchronized(this),而 main中
new Thread(new Test16(0,2,100)).start();
new Thread(new Test16(1,2,100)).start();
这样是2个不同的test16对象,因而是2个不同的锁,2个锁都在等待自己的资源(2个线程都停止了),所以不可能被唤醒。其实楼主想锁定的是共享的counter,counter的是static的,属于类对象,所以要在类上加锁才行。
代码修改如下:

  1. public void run() {  

  2.         synchronized (Test16.class) {  

  3.             while (counter < sum) {  

  4.                 while (turn != counter % num) {  

  5.                     try {  

  6.                         Test16.class.wait();  

  7.                     } catch (InterruptedException e) {  

  8.                         e.printStackTrace();  

  9.                     }  

  10.                 }  

  11.   

  12.                 counter++;  

  13.                 System.out.println(counter + "   turn : " + turn);  

  14.                 Test16.class.notifyAll();  

  15.   

  16.             }  

  17.         }  

  18.   

  19.     }  


查看完整回答
反对 回复 2022-10-06
?
慕慕森

TA贡献1856条经验 获得超17个赞

楼主,根本原因是你这里用了两个不同的对象,是不会实现互斥效果的

引用

new Thread(new Test16(0,2,100)).start();
new Thread(new Test16(1,2,100)).start();

这里两个Test16的对象,每个对象都可以进入你synchronized 的run方法。
这了互斥可以用类似生产者消费者李模式这种概念,或者直接使用阻塞队列,开始时队列只存放一个1,两个线程轮流每次在队列里取,取到消费后,将加1的结果再放入队列,此时notifyAll,另一个线程就可以取到数据。当前线程就会阻塞,以此可以实现你要的效果。


查看完整回答
反对 回复 2022-10-06
?
波斯汪

TA贡献1811条经验 获得超4个赞

while(turn!=counter%num){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

turn不等于的时候,然后就wait,
线程调用了wait,就会进入休眠状态并且释放锁,直到其他线程调用相同对象的notify或者notifyAll,这个线程才会(notify的话只是有可能会)重新进入执行队列。当这个线程开始执行的时候它会再次接管锁并执行wait后面的内容(接管锁这个动作会等待锁被其他线程释放)。

查看完整回答
反对 回复 2022-10-06
  • 3 回答
  • 0 关注
  • 142 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号