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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人国产在线播放 | 小莹的性荡生活45章 | 青青国产成人久久激情91麻豆 | 免费日本视频 | 日出水了特别黄的视频 | 国产视频播放 | 美女大乳被捏羞羞漫画 | 1024免费永久福利视频 | 国产一区二区三区四区波多野结衣 | 人人干国产 | 91超级碰| 精品一区二区三区五区六区 | 草溜短视频 | 欧美a在线观看 | 扒开女人下面 | chinese高中生gay男同 | 欧美一区不卡二区不卡三区 | 精品一区二区三区高清免费观看 | 国产亚洲成归v人片在线观看 | 国产日韩欧美在线播放 | 超强台风免费观看完整版视频 | 性直播免费 | 九九九九九九 | 亚洲欧美国产另类视频 | 午夜影院免费体验 | 国产精品久久久久毛片真精品 | 69人成网站色www | 男人亚洲天堂 | h肉动漫在线视频无修无遮挡 | 99热这里只精品99re66 | 国产成人青草视频 | 日韩欧美天堂 | 欧美一区二区三区精品影视 | 好妈妈7在线观看高清 | 亚洲高清无码在线 视频 | 国产99热99 | 天天综合网网欲色 | 日本网络视频www色高清免费 | 91久久99热青草国产 | 修修视频在线观看 | 美女翘臀内疯狂进出 |