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

JAVA線程sleep()和wait()詳解及實例

 更新時間:2017年05月12日 09:48:01   投稿:lqh  
這篇文章主要介紹了JAVA線程sleep()和wait()詳解及實例的相關(guān)資料,探討一下sleep()和wait()方法的區(qū)別和實現(xiàn)機制,需要的朋友可以參考下

JAVA線程sleep()和wait()詳解及實例

sleep

1.sleep是Thread的一個靜態(tài)(static)方法。使得Runnable實現(xiàn)的線程也可以使用sleep方法。而且避免了線程之前相互調(diào)用sleep()方法,引發(fā)死鎖。

2.sleep()執(zhí)行時需要賦予一個沉睡時間。在沉睡期間(阻塞線程期間),CPU會放棄這個線程,執(zhí)行其他任務(wù)。當沉睡時間到了之后,該線程會自動蘇醒,不過此時線程不會立刻被執(zhí)行,而是要等CPU分配資源,和其他線程進行競爭。

3.此外如果這個線程之前獲取了一個機鎖,在沉睡期間,這個機鎖不會釋放。其他等待這個機鎖的程序,必須等待這個線程醒來,且執(zhí)行完后才能運行。

sleep相關(guān)代碼

public class ThreadTest2 {

  public static void main(String[] args){
    System.out.println("begin our test");
    ThreadSleep sleep = new ThreadSleep();
    try {
      Thread thread1 = new Thread(sleep,"路人甲");
      Thread thread2 = new Thread(sleep,"路人乙");
      thread1.start();
      thread2.start();
    }catch(Exception e){
      e.printStackTrace();
    }
    System.out.println("test is over");
  }


}

 class ThreadSleep implements Runnable{

   int count = 0;

   @Override
   public void run(){
     System.out.println(Thread.currentThread().getName() + " say : hello sleep !!");
     count();

   }

   public void count(){
     while(count < 20) {
         System.out.println(Thread.currentThread().getName() + " say : count is " + count);
         try {
           count++;
           Thread.sleep(100);
         } catch (Exception e) {
           e.printStackTrace();
         }
     }

   }
}

輸出日志

begin our test
test is over
路人甲 say : hello sleep !!
路人甲 say : count is 0
路人乙 say : hello sleep !!
路人乙 say : count is 1
路人甲 say : count is 2
路人乙 say : count is 2
路人甲 say : count is 4
路人乙 say : count is 4
路人甲 say : count is 6
路人乙 say : count is 7
路人乙 say : count is 8
路人甲 say : count is 8
路人甲 say : count is 10
路人乙 say : count is 10
路人乙 say : count is 12
路人甲 say : count is 12
路人乙 say : count is 14
路人甲 say : count is 14
路人甲 say : count is 16
路人乙 say : count is 16
路人甲 say : count is 18
路人乙 say : count is 18

通過日志可以發(fā)現(xiàn)線程甲和線程乙基本是交替執(zhí)行,但是并不規(guī)律,且出現(xiàn)了并發(fā)問題。

該情況是由于代碼中設(shè)置了睡眠時間為100毫秒,由于count遞增執(zhí)行速度很快,所以線程差不多是同時睡眠,然后同時蘇醒并導致了并發(fā)的出現(xiàn)。

接下來要添加synchronize塊,檢查sleep時機鎖是否釋放

public class ThreadTest2 {

  public static void main(String[] args){
    System.out.println("begin our test");
    ThreadSleep sleep = new ThreadSleep();
    try {
      Thread thread1 = new Thread(sleep,"路人甲");
      Thread thread2 = new Thread(sleep,"路人乙");
      thread1.start();
      thread2.start();
    }catch(Exception e){
      e.printStackTrace();
    }
    System.out.println("test is over");
  }


}

class ThreadSleep implements Runnable{

  int count = 0;

  @Override
  public void run(){
    System.out.println(Thread.currentThread().getName() + " say : hello sleep !!");
    count();

  }

  public void count(){
    while(count < 20) {
      synchronized (this) {
        System.out.println(Thread.currentThread().getName() + " say : count is " + count);
        try {
          count++;
          Thread.sleep(100);
        } catch (Exception e) {
          e.printStackTrace();
        }

      }
    }

  }
}

輸出日志

begin our test
路人甲 say : hello sleep !!
路人甲 say : count is 0
test is over
路人乙 say : hello sleep !!
路人甲 say : count is 1
路人甲 say : count is 2
路人甲 say : count is 3
路人甲 say : count is 4
路人甲 say : count is 5
路人甲 say : count is 6
路人甲 say : count is 7
路人甲 say : count is 8
路人甲 say : count is 9
路人甲 say : count is 10
路人甲 say : count is 11
路人甲 say : count is 12
路人甲 say : count is 13
路人甲 say : count is 14
路人甲 say : count is 15
路人甲 say : count is 16
路人甲 say : count is 17
路人甲 say : count is 18
路人甲 say : count is 19
路人乙 say : count is 20

通過日志可以看出,基本是線程甲在執(zhí)行,這是因為sleep時,機鎖一直在線程甲上,所以線程乙只能一直等待直到線程甲釋放鎖。

wait

1.wait()是Object類的一個方法。當調(diào)用wait()方法時,該線程會進入和該對象相關(guān)的等待池中,并釋放它所擁有的機鎖。

2.執(zhí)行wait()后,必須使用notify()方法或notifyAll()方法或設(shè)置等待時間(wait(long time))喚醒在等待線程池中的線程。

3.wait()必須放在synchronized block中,否則會在運行時報“java.lang.IllegalMonitorStateException”異常

wait相關(guān)代碼

public class ThreadTest2 {

  public static void main(String[] args) {
    System.out.println("begin our test");
    ThreadSleep sleep = new ThreadSleep();
    try {
      Thread thread1 = new Thread(sleep, "路人甲");
      Thread thread2 = new Thread(sleep, "路人乙");
      thread1.start();
      thread2.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println("test is over");
  }
}

class ThreadSleep implements Runnable {

  int count = 0;

  @Override
  public void run() {
    System.out.println(Thread.currentThread().getName() + " say : hello sleep !!");
    count();

  }

  public void count() {
    while (count < 20) {
      synchronized (this) {
        System.out.println(Thread.currentThread().getName() + " say : count is " + count);
        try {
          count++;
          this.wait(100);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

  }
}

輸出日志

begin our test
路人甲 say : hello sleep !!
路人甲 say : count is 0
test is over
路人乙 say : hello sleep !!
路人乙 say : count is 1
路人甲 say : count is 2
路人乙 say : count is 3
路人甲 say : count is 4
路人乙 say : count is 5
路人甲 say : count is 6
路人乙 say : count is 7
路人甲 say : count is 8
路人乙 say : count is 9
路人甲 say : count is 10
路人乙 say : count is 11
路人甲 say : count is 12
路人乙 say : count is 13
路人乙 say : count is 14
路人甲 say : count is 15
路人乙 say : count is 16
路人甲 say : count is 17
路人乙 say : count is 18
路人甲 say : count is 19

通過日志可以發(fā)現(xiàn)在wait的情況下,機鎖會被釋放。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Java實現(xiàn)的時間戳與date對象相互轉(zhuǎn)換功能示例

    Java實現(xiàn)的時間戳與date對象相互轉(zhuǎn)換功能示例

    這篇文章主要介紹了Java實現(xiàn)的時間戳與date對象相互轉(zhuǎn)換功能,結(jié)合具體實例形式分析了java日期與時間戳類型的表示與轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • Maven中的SnapShot版本和正式版本的區(qū)別

    Maven中的SnapShot版本和正式版本的區(qū)別

    在Nexus倉庫中,一個倉庫一般分為public(Release)倉和SNAPSHOT倉,本文詳細的介紹了SnapShot版本和正式版本的區(qū)別,感興趣的可以了解一下
    2021-06-06
  • spring?boot只需兩步優(yōu)雅整合activiti示例解析

    spring?boot只需兩步優(yōu)雅整合activiti示例解析

    這篇文章主要主要來教大家spring?boot優(yōu)雅整合activiti只需兩步就可完成測操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進步
    2022-03-03
  • Spring Boot2.x集成JPA快速開發(fā)的示例代碼

    Spring Boot2.x集成JPA快速開發(fā)的示例代碼

    這篇文章主要介紹了Spring Boot2.x集成JPA快速開發(fā),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • 解析Java中PriorityQueue優(yōu)先級隊列結(jié)構(gòu)的源碼及用法

    解析Java中PriorityQueue優(yōu)先級隊列結(jié)構(gòu)的源碼及用法

    優(yōu)先級隊列是一種隊列結(jié)構(gòu),是0個或多個元素的集合,每個元素都有一個優(yōu)先權(quán),PriorityQueue被內(nèi)置于JDK中,本文就來解析Java中PriorityQueue優(yōu)先級隊列結(jié)構(gòu)的源碼及用法.
    2016-05-05
  • SpringMVC深入講解文件的上傳下載實現(xiàn)

    SpringMVC深入講解文件的上傳下載實現(xiàn)

    這篇文章主要為大家詳細介紹了springMVC實現(xiàn)文件上傳和下載的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java?天生就是多線程

    Java?天生就是多線程

    這篇文章主要介紹了Java天生就是多線程,程序天生就是多線程程序,因為執(zhí)行main()方法的是一個名稱為main的線程,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-07-07
  • springboot快速整合Mybatis組件的方法(推薦)

    springboot快速整合Mybatis組件的方法(推薦)

    Spring Boot是由Pivotal團隊提供的全新框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。這篇文章主要介紹了springboot快速整合Mybatis組件的方法,需要的朋友可以參考下
    2019-11-11
  • SpringBoot整合SpringSecurity認證與授權(quán)

    SpringBoot整合SpringSecurity認證與授權(quán)

    在項目開發(fā)中,權(quán)限認證是很重要的,尤其是一些管理類的系統(tǒng),對于權(quán)限要求更為嚴格,本文主要介紹了SpringBoot整合SpringSecurity認證與授權(quán),感興趣的可以了解一下
    2023-11-11
  • Java JVM內(nèi)存區(qū)域詳解

    Java JVM內(nèi)存區(qū)域詳解

    下面小編就為大家?guī)硪黄趈vm java內(nèi)存區(qū)域的介紹。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-11-11

最新評論