學習時正好碰到這兩個方法,就查閱相關資料,並通過程序實現,進行區別一下:
1、每個對象都有一個鎖來控制同步訪問,Synchronized關鍵字可以和對象的鎖交互,來實現同步方法或同步塊。sleep()方法正在執行的線程主動讓出CPU(然後CPU就可以去執行其他任務),在sleep指定時間後CPU再回到該線程繼續往下執行(注意:sleep方法只讓出了CPU,而並不會釋放同步資源鎖!!!);wait()方法則是指當前線程讓自己暫時退讓出同步資源鎖,以便其他正在等待該資源的線程得到該資源進而運行,只有調用了notify()方法,之前調用wait()的線程纔會解除wait狀態,可以去參與競爭同步資源鎖,進而得到執行。(注意:notify的作用相當於叫醒睡着的人,而並不會給他分配任務,就是說notify只是讓之前調用wait的線程有權利重新參與線程的調度);
2、sleep()方法可以在任何地方使用;wait()方法則只能在同步方法或同步塊中使用;
3、sleep()是線程線程類(Thread)的方法,調用會暫停此線程指定的時間,但監控依然保持,不會釋放對象鎖,到時間自動恢復;wait()是Object的方法,調用會放棄對象鎖,進入等待隊列,待調用notify()/notifyAll()喚醒指定的線程或者所有線程,纔會進入鎖池,不再次獲得對象鎖纔會進入運行狀態;
以程序說明:
-
public class MultiThread {
-
-
private static class Thread1 implements Runnable{
-
@Override
-
public void run() {
-
-
-
synchronized(MultiThread.class){
-
System.out.println("enter thread1 ...");
-
System.out.println("thread1 is waiting");
-
-
try{
-
-
-
MultiThread.class.wait();
-
}catch(InterruptedException e){
-
e.printStackTrace();
-
}
-
-
System.out.println("thread1 is going on ...");
-
System.out.println("thread1 is being over!");
-
}
-
}
-
-
}
-
-
private static class Thread2 implements Runnable{
-
@Override
-
public void run() {
-
-
-
synchronized(MultiThread.class){
-
System.out.println("enter thread2 ...");
-
System.out.println("thread2 notify other thread can release wait status ...");
-
MultiThread.class.notify();
-
System.out.println("thread2 is sleeping ten millisecond ...");
-
-
try{
-
Thread.sleep(10);
-
}catch(InterruptedException e){
-
e.printStackTrace();
-
}
-
-
System.out.println("thread2 is going on ...");
-
System.out.println("thread2 is being over!");
-
}
-
}
-
}
-
-
public static void main(String[] args) {
-
new Thread(new Thread1()).start();
-
try{
-
Thread.sleep(10);
-
}catch(InterruptedException e){
-
e.printStackTrace();
-
}
-
-
new Thread(new Thread2()).start();
-
}
-
-
}
程序運行結果如下圖所示