一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - Java多線程中關于join方法的使用實例解析

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

2020-07-24 12:2452Hz JAVA教程

本文通過實例代碼給大家實例介紹了Java多線程中關于join方法的使用,非常不錯,具有參考借鑒價值,需要的朋友參考下

先上代碼

新建一個Thread,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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();
  }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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方法的解釋:

?
1
2
3
4
5
6
7
8
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,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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();
  }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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方法的解釋:

?
1
2
3
4
5
6
7
8
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方法的線程,其他線程等待該線程完全執行完后才會進來。

原文鏈接:http://www.cnblogs.com/Miracle-Maker/archive/2017/01/08/6261738.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女扒开腿让男人桶爽免费gif | 国产成人精品一区二区阿娇陈冠希 | 操骚0| 欧美日韩亚洲国内综合网香蕉 | 小小水蜜桃视频高清在线播放 | 国产自拍啪啪 | 操穴片| 午夜影院免费观看视频 | 高清国产在线观看 | 日产精品卡一卡2卡三卡乱码工厂 | 亚洲精品无码久久不卡 | 15一16japanese破| 4hc44四虎永久地址链接 | 精品国语对白精品自拍视 | 九九热在线视频观看这里只有精品 | 成人国产在线视频在线观看 | 欧美同性videos| 国产免费专区 | 好男人好资源在线观看 | 性奴公司 警花 | 513热点| 国产精品视频自拍 | 欧美在线国产 | 欧洲一级黑寡妇 | 欧美一级专区免费大片 | 我把校花黑色蕾丝胸罩脱了 | 国产自拍资源 | 99热这里只有精 | 四虎国产精品免费久久久 | 青青国产在线视频 | 好男人资源在线观看免费的 | 久久草香蕉频线观 | 拔插拔插8x8x海外华人免费视频 | 国产成人亚洲综合a∨婷婷 国产成人亚洲精品乱码在线观看 | 桥本有菜在线四虎福利网 | 男人捅女人漫画 | 日韩在线免费 | 日本色播 | 美女脱了内裤打开腿让人羞羞软件 | 美女撒尿无遮挡免费中国 | 精品综合久久久久久8888 |