欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java sleep()和wait()的區(qū)別點總結(jié)

 更新時間:2021年04月29日 08:27:32   作者:小妮淺淺  
在本篇文章里小編給大家整理了一篇關(guān)于java sleep()和wait()的區(qū)別的相關(guān)內(nèi)容,有興趣的朋友們可以學習下。

1、區(qū)別說明

wait()是Object的方法,sleep()是Thread的方法。

wait()必須采用同步方法,不需要sleep()方法。

線程在同步方法中執(zhí)行sleep()方法,不釋放monitor鎖,wait()方法釋放monitor鎖。

短暫休眠后,sleep()方法會主動退出阻塞,而wait()方法需要在沒有指定wait時間的情況下被其他線程中斷才能退出阻塞。

2、實例

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");
}
}
}

內(nèi)容擴展:

/**
 * 
 */
package com.b510.test;

/**
 * java中的sleep()和wait()的區(qū)別
 * @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 {
        //調(diào)用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....");
        //只有針對此對象調(diào)用notify()方法后本線程才進入對象鎖定池準備獲取對象鎖進入運行狀態(tài)。
        TestD.class.notify();
        //==================
        //區(qū)別
        //如果我們把代碼:TestD.class.notify();給注釋掉,即TestD.class調(diào)用了wait()方法,但是沒有調(diào)用notify()
        //方法,則線程永遠處于掛起狀態(tài)。
        try {
          //sleep()方法導(dǎo)致了程序暫停執(zhí)行指定的時間,讓出cpu該其他線程,
          //但是他的監(jiān)控狀態(tài)依然保持者,當指定的時間到了又會自動恢復(fù)運行狀態(tài)。
          //在調(diào)用sleep()方法的過程中,線程不會釋放對象鎖。
          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ū)別點總結(jié)的文章就介紹到這了,更多相關(guān)java sleep()和wait()的區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于StringUtils.isBlank()的使用及說明

    關(guān)于StringUtils.isBlank()的使用及說明

    這篇文章主要介紹了關(guān)于StringUtils.isBlank()的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • SpringBoot2 整合MinIO中間件實現(xiàn)文件便捷管理功能

    SpringBoot2 整合MinIO中間件實現(xiàn)文件便捷管理功能

    這篇文章主要介紹了SpringBoot2 整合MinIO中間件,實現(xiàn)文件便捷管理,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • Springboot項目通過redis實現(xiàn)接口的冪等性

    Springboot項目通過redis實現(xiàn)接口的冪等性

    這篇文章主要為大家介紹了Springboot項目通過redis實現(xiàn)接口的冪等性,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • IDEA中創(chuàng)建properties配置文件

    IDEA中創(chuàng)建properties配置文件

    我們在j2ee當中,連接數(shù)據(jù)庫的時候經(jīng)常會用到properties配置文件,本文主要介紹了IDEA中創(chuàng)建properties配置文件,具有一定的參考價值,?感興趣的可以了解一下
    2024-04-04
  • Spring IOC:CreateBean環(huán)節(jié)中的流程轉(zhuǎn)換

    Spring IOC:CreateBean環(huán)節(jié)中的流程轉(zhuǎn)換

    Spring IOC 體系是一個很值得深入和研究的結(jié)構(gòu) , 只有自己真正的讀一遍 , 才能有更好的理解.這篇文章主要說明一下 CreateBean 整個環(huán)節(jié)中的大流程轉(zhuǎn)換 , 便于查找問題的原因
    2021-05-05
  • Java代碼讀取文件緩存問題解決

    Java代碼讀取文件緩存問題解決

    最近遇到了一個Java文件讀取的緩存問題,打遠程斷點出現(xiàn)的也是原來的老代碼參數(shù),本文就介紹一下解決方法,感興趣的可以了解一下
    2021-05-05
  • spring?boot?實現(xiàn)一個?禁止重復(fù)請求的方法

    spring?boot?實現(xiàn)一個?禁止重復(fù)請求的方法

    這篇文章主要介紹了spring?boot?實現(xiàn)一個?禁止重復(fù)請求,當重復(fù)請求該方法時,會返回"Duplicate?request",避免重復(fù)執(zhí)行相同的操作,需要的朋友可以參考下
    2024-03-03
  • 使用Java8進行分組(多個字段的組合分組)

    使用Java8進行分組(多個字段的組合分組)

    本文主要介紹了使用Java8進行分組(多個字段的組合分組),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • Java黑盒測試之nextDate函數(shù)測試

    Java黑盒測試之nextDate函數(shù)測試

    這篇文章主要介紹了Java黑盒測試之nextDate函數(shù)測試,文中有非常詳細的代碼示例,對正在學習Java黑盒測試的小伙伴們有很大的幫助哦,需要的朋友可以參考下
    2021-05-05
  • Java中的CompletableFuture異步編程詳解

    Java中的CompletableFuture異步編程詳解

    這篇文章主要介紹了Java中的CompletableFuture異步編程詳解,只要提到多線程來優(yōu)化性能,那么必定離不開異步化,異步化的出現(xiàn)才是多線程優(yōu)化性能這個核心方案的基礎(chǔ),需要的朋友可以參考下
    2023-12-12

最新評論