這篇文章主要介紹java中sleep()和wait()有什么區別,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Java是一門面向對象編程語言,可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序。
wait()是Object的方法,sleep()是Thread的方法。
wait()必須采用同步方法,不需要sleep()方法。
線程在同步方法中執行sleep()方法,不釋放monitor鎖,wait()方法釋放monitor鎖。
短暫休眠后,sleep()方法會主動退出阻塞,而wait()方法需要在沒有指定wait時間的情況下被其他線程中斷才能退出阻塞。
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleepAndWait {
public static void main(String[] args) {
new Thread1().start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread2().start();
}
}
class Thread1 extends Thread{
private void sout(String s){
System.out.println(s+" "+new SimpleDateFormat("HH:mm:ss:SS").format(new Date()));
}
@Override
public void run() {
sout("enter Thread1.run");
synchronized (TestSleepAndWait.class){//wait只能在同步代碼塊或者同步方法中使用
sout("Thread1 is going to wait");
try {
TestSleepAndWait.class.wait(); // 這里只能使用持有鎖TestSleepAndWait.class.wait(),使用其他對象則報錯java.lang.IllegalMonitorStateException
} catch (InterruptedException e) {
e.printStackTrace();
}
sout("after waiting, thread1 is going on");
sout("thread1 is over");
}
}
}
class Thread2 extends Thread{
private void sout(String s){
System.out.println(s+" "+new SimpleDateFormat("HH:mm:ss:SS").format(new Date()));
}
@Override
public void run() {
sout("enter Thread2.run");
synchronized (TestSleepAndWait.class){//wait只能在同步代碼塊或者同步方法中使用
sout("Thread2 is going to notify");
TestSleepAndWait.class.notify(); 這里只能使用持有鎖TestSleepAndWait.class
sout("thread2 is going to sleep 10ms");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
sout("after sleeping, thread2 is going on");
sout("thread2 is over");
}
}
}內容擴展:
/**
*
*/
package com.b510.test;
/**
* java中的sleep()和wait()的區別
* @author Hongten Java學習交流QQ群:589809992 我們一起學Java!
* @date 2013-12-10
*/
public class TestD {
public static void main(String[] args) {
new Thread(new Thread1()).start();
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
new Thread(new Thread2()).start();
}
private static class Thread1 implements Runnable{
@Override
public void run(){
synchronized (TestD.class) {
System.out.println("enter thread1...");
System.out.println("thread1 is waiting...");
try {
//調用wait()方法,線程會放棄對象鎖,進入等待此對象的等待鎖定池
TestD.class.wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread1 is going on ....");
System.out.println("thread1 is over!!!");
}
}
}
private static class Thread2 implements Runnable{
@Override
public void run(){
synchronized (TestD.class) {
System.out.println("enter thread2....");
System.out.println("thread2 is sleep....");
//只有針對此對象調用notify()方法后本線程才進入對象鎖定池準備獲取對象鎖進入運行狀態。
TestD.class.notify();
//==================
//區別
//如果我們把代碼:TestD.class.notify();給注釋掉,即TestD.class調用了wait()方法,但是沒有調用notify()
//方法,則線程永遠處于掛起狀態。
try {
//sleep()方法導致了程序暫停執行指定的時間,讓出cpu該其他線程,
//但是他的監控狀態依然保持者,當指定的時間到了又會自動恢復運行狀態。
//在調用sleep()方法的過程中,線程不會釋放對象鎖。
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread2 is going on....");
System.out.println("thread2 is over!!!");
}
}
}
}以上是“java中sleep()和wait()有什么區別”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。