最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java多線程中關(guān)于join方法的使用實(shí)例解析

 更新時(shí)間:2017年01月08日 14:32:48   作者:52Hz  
本文通過(guò)實(shí)例代碼給大家實(shí)例介紹了Java多線程中關(guān)于join方法的使用,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下

先上代碼

新建一個(gè)Thread,代碼如下:

package com.thread.test;
public class MyThread extends Thread {
  private String name;
  public MyThread(String name) {
    this.name = name;
  }
  @Override
  public void run() {
    for (int i = 0; i < 100; i++) {
      System.out.println(name+"["+i+"]");
    }
    super.run();
  }
}

之后新建測(cè)試類,代碼如下:

package com.thread.test;
/*
 * 0-50執(zhí)行的是主線程,50-100執(zhí)行的是A線程,并且將A線程完全執(zhí)行完后才繼續(xù)執(zhí)行主線程
 */
public class ThreadDemo{
  public static void main(String[] args) {
    MyThread t = new MyThread("A");
    t.start();
    for (int i = 0; i < 100; i++) {
      if (i>50) {
        try {
          t.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("主線程"+"["+i+"]");
    }
  }
}

下面是Java Platform SE8 API中對(duì)Thread中Join方法的解釋:

public final void join(long millis)
        throws InterruptedExceptionWaits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever. 
This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
Parameters: 
millis - the time to wait in milliseconds 
Throws: 
IllegalArgumentException - if the value of millis is negative 
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

先上代碼

新建一個(gè)Thread,代碼如下:

package com.thread.test;
public class MyThread extends Thread {
  private String name;
  public MyThread(String name) {
    this.name = name;
  }
  @Override
  public void run() {
    for (int i = 0; i < 100; i++) {
      System.out.println(name+"["+i+"]");
    }
    super.run();
  }
}

之后新建測(cè)試類,代碼如下:

package com.thread.test;
/*
 * 0-50執(zhí)行的是主線程,50-100執(zhí)行的是A線程,并且將A線程完全執(zhí)行完后才繼續(xù)執(zhí)行主線程
 */
public class ThreadDemo{
  public static void main(String[] args) {
    MyThread t = new MyThread("A");
    t.start();
    for (int i = 0; i < 100; i++) {
      if (i>50) {
        try {
          t.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("主線程"+"["+i+"]");
    }
  }
}

下面是Java Platform SE8 API中對(duì)Thread中Join方法的解釋:

public final void join(long millis)
        throws InterruptedExceptionWaits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever. 
This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
Parameters: 
millis - the time to wait in milliseconds 
Throws: 
IllegalArgumentException - if the value of millis is negative 
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

 我自己的理解就是會(huì)強(qiáng)行進(jìn)入使用join方法的線程,其他線程等待該線程完全執(zhí)行完后才會(huì)進(jìn)來(lái)。

相關(guān)文章

  • Spring?boot2.0?實(shí)現(xiàn)日志集成的方法(3)

    Spring?boot2.0?實(shí)現(xiàn)日志集成的方法(3)

    這篇文章主要介紹了Spring?boot2.0?實(shí)現(xiàn)日志集成的方法,基于上一篇將日志信息根據(jù)類別輸出到不同的文件中,這篇文章將通過(guò)日志來(lái)監(jiān)控用戶的操作行為、請(qǐng)求的耗時(shí)情況,針對(duì)耗時(shí)久的請(qǐng)求進(jìn)行性能分析,提升系統(tǒng)性能,需要的小伙伴可以參考一下
    2022-04-04
  • java中File類的構(gòu)造函數(shù)及其方法

    java中File類的構(gòu)造函數(shù)及其方法

    這篇文章主要介紹了java中File類的構(gòu)造函數(shù)及其方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-06-06
  • 如何解決Eclipse找不到或無(wú)法加載主類問(wèn)題

    如何解決Eclipse找不到或無(wú)法加載主類問(wèn)題

    這篇文章主要介紹了如何解決Eclipse找不到或無(wú)法加載主類問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Java編程倒計(jì)時(shí)實(shí)現(xiàn)方法示例

    Java編程倒計(jì)時(shí)實(shí)現(xiàn)方法示例

    這篇文章主要介紹了Java編程倒計(jì)時(shí)實(shí)現(xiàn)的三個(gè)示例,三種實(shí)現(xiàn)方法,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-09-09
  • SpringBoot集成Mybatis的實(shí)現(xiàn)步驟

    SpringBoot集成Mybatis的實(shí)現(xiàn)步驟

    這篇文章主要介紹了SpringBoot集成Mybatis的實(shí)現(xiàn)步驟,本文通過(guò)SpringBoot +MyBatis 實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)學(xué)生表的查詢操作,需要的朋友可以參考下
    2020-12-12
  • 源碼分析Java中ThreadPoolExecutor的底層原理

    源碼分析Java中ThreadPoolExecutor的底層原理

    這篇文章主要帶大家從源碼分析一下Java中ThreadPoolExecutor的底層原理,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下
    2023-05-05
  • Java中的內(nèi)存泄露問(wèn)題和解決辦法

    Java中的內(nèi)存泄露問(wèn)題和解決辦法

    大家好,本篇文章主要講的是Java中的內(nèi)存泄露問(wèn)題和解決辦法,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • Java?將PDF轉(zhuǎn)為HTML時(shí)保存到流的方法和步驟

    Java?將PDF轉(zhuǎn)為HTML時(shí)保存到流的方法和步驟

    本文介紹如何通過(guò)Java后端程序代碼將PDF文件轉(zhuǎn)為HTML,并將轉(zhuǎn)換后的HTML文件保存到流,下面是實(shí)現(xiàn)轉(zhuǎn)換的方法和步驟,感興趣的朋友一起看看吧
    2022-01-01
  • springboot項(xiàng)目配置logback日志系統(tǒng)的實(shí)現(xiàn)

    springboot項(xiàng)目配置logback日志系統(tǒng)的實(shí)現(xiàn)

    這篇文章主要介紹了springboot項(xiàng)目配置logback日志系統(tǒng)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Java如何基于DOM解析xml文件

    Java如何基于DOM解析xml文件

    這篇文章主要介紹了Java如何基于DOM解析xml文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09

最新評(píng)論

洛浦县| 永吉县| 寻乌县| 荔浦县| 普兰店市| 贺州市| 崇文区| 化州市| 云南省| 济阳县| 莲花县| 年辖:市辖区| 读书| 中超| 玉树县| 长岭县| 射洪县| 搜索| 饶平县| 虎林市| 垫江县| 图木舒克市| 弥勒县| 保德县| 平果县| 聂拉木县| 平陆县| 定陶县| 金秀| 六安市| 新余市| 台南县| 宜君县| 大连市| 吐鲁番市| 平塘县| 平安县| 湄潭县| 东莞市| 阳春市| 怀安县|