国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java多線程中關于join方法的使用實例解析

2019-11-26 13:17:52
字體:
來源:轉載
供稿:網友

先上代碼

新建一個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();  }}

之后新建測試類,代碼如下:

package com.thread.test;/* * 0-50執行的是主線程,50-100執行的是A線程,并且將A線程完全執行完后才繼續執行主線程 */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中對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.

先上代碼

新建一個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();  }}

之后新建測試類,代碼如下:

package com.thread.test;/* * 0-50執行的是主線程,50-100執行的是A線程,并且將A線程完全執行完后才繼續執行主線程 */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中對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.

 我自己的理解就是會強行進入使用join方法的線程,其他線程等待該線程完全執行完后才會進來。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐陵市| 德清县| 襄城县| 绥芬河市| 松潘县| 根河市| 左权县| 南雄市| 平阴县| 府谷县| 宜兰县| 涞水县| 招远市| 遂昌县| 靖安县| 雷波县| 砀山县| 门源| 博乐市| 贵阳市| 三明市| 侯马市| 武强县| 江口县| 酉阳| 和林格尔县| 邻水| 万荣县| 元阳县| 秦皇岛市| 桑植县| 南澳县| 莲花县| 吕梁市| 武功县| 三穗县| 三穗县| 遵义县| 资源县| 聂拉木县| 阿拉善盟|