java sleep()和wait()的區(qū)別點(diǎn)總結(jié)
1、區(qū)別說(shuō)明
wait()是Object的方法,sleep()是Thread的方法。
wait()必須采用同步方法,不需要sleep()方法。
線程在同步方法中執(zhí)行sleep()方法,不釋放monitor鎖,wait()方法釋放monitor鎖。
短暫休眠后,sleep()方法會(huì)主動(dòng)退出阻塞,而wait()方法需要在沒(méi)有指定wait時(shí)間的情況下被其他線程中斷才能退出阻塞。
2、實(shí)例
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(),使用其他對(duì)象則報(bào)錯(cuò)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");
}
}
}
內(nèi)容擴(kuò)展:
/**
*
*/
package com.b510.test;
/**
* java中的sleep()和wait()的區(qū)別
* @author Hongten Java學(xué)習(xí)交流QQ群:589809992 我們一起學(xué)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 {
//調(diào)用wait()方法,線程會(huì)放棄對(duì)象鎖,進(jìn)入等待此對(duì)象的等待鎖定池
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....");
//只有針對(duì)此對(duì)象調(diào)用notify()方法后本線程才進(jìn)入對(duì)象鎖定池準(zhǔn)備獲取對(duì)象鎖進(jìn)入運(yùn)行狀態(tài)。
TestD.class.notify();
//==================
//區(qū)別
//如果我們把代碼:TestD.class.notify();給注釋掉,即TestD.class調(diào)用了wait()方法,但是沒(méi)有調(diào)用notify()
//方法,則線程永遠(yuǎn)處于掛起狀態(tài)。
try {
//sleep()方法導(dǎo)致了程序暫停執(zhí)行指定的時(shí)間,讓出cpu該其他線程,
//但是他的監(jiān)控狀態(tài)依然保持者,當(dāng)指定的時(shí)間到了又會(huì)自動(dòng)恢復(fù)運(yùn)行狀態(tài)。
//在調(diào)用sleep()方法的過(guò)程中,線程不會(huì)釋放對(duì)象鎖。
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread2 is going on....");
System.out.println("thread2 is over!!!");
}
}
}
}
到此這篇關(guān)于java sleep()和wait()的區(qū)別點(diǎn)總結(jié)的文章就介紹到這了,更多相關(guān)java sleep()和wait()的區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 結(jié)構(gòu)化并發(fā)Structured Concurrency實(shí)踐舉例
Java21結(jié)構(gòu)化并發(fā)通過(guò)作用域和任務(wù)句柄統(tǒng)一管理并發(fā)生命周期,解決線程泄漏與任務(wù)追蹤問(wèn)題,提升代碼安全性和可觀測(cè)性,其核心目標(biāo)為資源整合、依賴管理、異??刂?適用于復(fù)雜并發(fā)場(chǎng)景,未來(lái)將與JVM集成并擴(kuò)展框架支持,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-09-09
認(rèn)識(shí)Java底層操作系統(tǒng)與并發(fā)基礎(chǔ)
這篇文章主要介紹了認(rèn)識(shí)Java底層操作系統(tǒng)與并發(fā)基礎(chǔ),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
Java元素排序Comparable與Comparator的區(qū)別
這篇文章主要介紹了Java元素排序Comparable與Comparator的區(qū)別,二者都是頂級(jí)的接口,但擁有的方法和用法是不同的,下面我們分別來(lái)看看具體是怎樣的區(qū)別吧2022-05-05
javaweb實(shí)戰(zhàn)之商城項(xiàng)目開發(fā)(三)
這篇文章主要針對(duì)javaweb商城項(xiàng)目開發(fā)進(jìn)行實(shí)戰(zhàn)演習(xí),主要實(shí)現(xiàn)通用的BaseDao.java和使用resultMap映射關(guān)聯(lián)對(duì)象,感興趣的小伙伴們可以參考一下2016-02-02
Spring容器的創(chuàng)建過(guò)程之如何注冊(cè)BeanPostProcessor詳解
關(guān)于BeanPostProcessor 各位一定不陌生,今天整理的這篇文章總結(jié)了如何注冊(cè)BeanPostProcessor,文中有非常詳細(xì)的圖文示例,需要的朋友可以參考下2021-06-06

