欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java多线程中关于join方法的使用实例解析

程序员文章站 2024-03-08 13:19:40
先上代码 新建一个thread,代码如下: package com.thread.test; public class mythread extends th...

先上代码

新建一个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方法的线程,其他线程等待该线程完全执行完后才会进来。