亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以下程序為了學習而寫,但是經常發生死鎖,請問為什么?

以下程序為了學習而寫,但是經常發生死鎖,請問為什么?

滄海一幻覺 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 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號