線程同步2——同步通信wait和notify
一、線程如何停止
使用stop()不安全。它會解除由線程獲取的所有鎖定,而且如果對象處於一種不連貫狀態,那麼其他線程能在那種狀態下檢查和修改它們。結果很難檢查出真正的問題所在。suspend()方法容易發生死鎖。調用suspend()的時候,目標線程會停下來,但卻仍然持有在這之前獲得的鎖定。此時其他任何線程都不能訪問鎖定的資源,除非被"掛起"的線程恢複運行。對任何線程來說,如果它們想恢複目標線程,同時又試圖使用任何一個鎖定的資源,就會造成死鎖。所以不應該使用suspend()。正確的做法是而應在自己的Thread類中置入一個標誌,指出線程應該活動還是掛起。若標誌指出線程應該掛起,便用wait()命其進入等待狀態。若標誌指出線程應當恢複,則用一個notify()重新啟動線程。
二、wait和notify實例
子線程循環10次,主線程循環100次。接著子線程循環10次,主線程循環100次。如此循環50次。摘自張孝祥老師線程視頻源碼。
public class TraditionalThreadCommunication
{
public static void main(String[] args)
{
final Business business = new Business();
new Thread(new Runnable()
{
public void run()
{
for (int i = 1; i <= 50; i++)
{
business.sub(i);
}
}
}).start();
for (int i = 1; i <= 50; i++)
{
business.main(i);
}
}
}
class Business
{
private boolean bShouldSub = true;
public synchronized void sub(int i)
{
while (!bShouldSub)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
for (int j = 1; j <= 10; j++)
{
System.out.println("sub thread sequence of " + j + ",loop of " + i);
}
bShouldSub = false;
this.notify();
}
public synchronized void main(int i)
{
while (bShouldSub)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
for (int j = 1; j <= 100; j++)
{
System.out.println("main thread sequence of " + j + ",loop of " + i);
}
bShouldSub = true;
this.notify();
}
}
最後更新:2017-04-03 16:49:16