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

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)文章

  • Springboot使用Security實(shí)現(xiàn)OAuth2授權(quán)驗(yàn)證完整過(guò)程

    Springboot使用Security實(shí)現(xiàn)OAuth2授權(quán)驗(yàn)證完整過(guò)程

    安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會(huì)發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會(huì)出現(xiàn)問(wèn)題,這篇文章主要介紹了SpringBoot使用Security實(shí)現(xiàn)OAuth2授權(quán)驗(yàn)證完整過(guò)程
    2022-12-12
  • IntelliJ IDEA(2017)安裝和破解的方法

    IntelliJ IDEA(2017)安裝和破解的方法

    這篇文章主要介紹了IntelliJ IDEA(2017)安裝和破解的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • JAVA基本概念詳解

    JAVA基本概念詳解

    本文主要介紹了Java的基本概念。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧,希望能夠給你帶來(lái)幫助
    2021-11-11
  • Java深入淺出說(shuō)流的使用

    Java深入淺出說(shuō)流的使用

    這篇文章主要介紹了Java深入淺出說(shuō)流的使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Spring中Bean創(chuàng)建完后打印語(yǔ)句的兩種方法

    Spring中Bean創(chuàng)建完后打印語(yǔ)句的兩種方法

    這篇文章主要介紹了Spring中Bean創(chuàng)建完后打印語(yǔ)句的兩種方法,一個(gè)是實(shí)現(xiàn)InitializingBean接口,另一個(gè)使用@Bean注解和initMethod屬性,通過(guò)代碼示例介紹的非常詳細(xì),感興趣的小伙伴可以參考閱讀
    2023-07-07
  • 在Struts2中如何將父類屬性序列化為JSON格式的解決方法

    在Struts2中如何將父類屬性序列化為JSON格式的解決方法

    本篇文章,小編將為大家介紹關(guān)于在Struts2中如何將父類屬性序列化為JSON格式的解決方法,有需要的朋友可以參考一下
    2013-04-04
  • springboot 如何解決cross跨域請(qǐng)求的問(wèn)題

    springboot 如何解決cross跨域請(qǐng)求的問(wèn)題

    這篇文章主要介紹了springboot 如何解決cross跨域請(qǐng)求的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決

    mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決

    這篇文章主要介紹了mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 使用jps命令查看Java進(jìn)程的詳細(xì)指南

    使用jps命令查看Java進(jìn)程的詳細(xì)指南

    jps是Java開(kāi)發(fā)者和系統(tǒng)管理員的得力助手,它簡(jiǎn)化了Java進(jìn)程監(jiān)控的過(guò)程,使得快速檢查應(yīng)用運(yùn)行狀態(tài)變得輕而易舉,在Java開(kāi)發(fā)和運(yùn)維場(chǎng)景中,jps是一個(gè)非常實(shí)用的命令行工具,本文介紹了如何有效地使用 jps命令來(lái)查看Java進(jìn)程的詳細(xì)指南,需要的朋友可以參考下
    2024-10-10
  • Java AES加密解密的簡(jiǎn)單實(shí)現(xiàn)方法

    Java AES加密解密的簡(jiǎn)單實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇Java AES加密解密的簡(jiǎn)單實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06

最新評(píng)論