首页 / 操作系统 / Linux / Java 线程协作 join()
在实际开发中我们往往会遇到这样的情况一个线程的执行需要依赖另一个线程执行后的结果。即主线程生成并起动了子线程,如果子线程里要进行大量的耗时的运算,主线程往往将于子线程之前结束,但是如果主线程处理完其他的事务后,需要用到子线程的处理结果,也就是主线程需要等待子线程执行完成之后再结束,这个时候就要用到join()方法了。
join()
join() 方法主要是让调用该方法的thread完成run方法里面的任务后, 再执行join()方法后面的代。主线程生成并起动了子线程,而子线程里要进行大量的耗时的运算(这里可以借鉴下线程的作用),当主线程处理完其他的事务后,需要用到子线程的处理结果,这个时候就要用到join();方法了。//该方法会无限等待,它会一直阻塞当前线程直到目标线程(调用该方法的线程)执行完毕。
public final void join() throws InterruptedException {join(0);}//该方法需要给出最大等待时间,如果目标线程的执行时间超过给定的时间,那么当前线程将不在等待目标线程执行结束而直接执行public final synchronized void join(long millis)throws InterruptedException{} public class ThreadJoinTest {private static Stringstr=null;public static void main(String[] args) {Thread thread=new Thread(){@Overridepublic void run() {try {System.out.println("进入"+Thread.currentThread().getName()+"线程");Thread.sleep(1000);str="hello Word";System.out.println(Thread.currentThread().getName()+"线程业务处理完毕");} catch (InterruptedException e) {e.printStackTrace();}}};thread.start();try {//thread.join();//设置main线程等待子线程先处理业务System.out.println(Thread.currentThread().getName()+"线程处理业务开始");System.out.println("获取str值:"+str);} catch (Exception e) {e.printStackTrace();}}}
执行结果:
main线程处理业务开始
进入Thread-0线程
获取str值:null
Thread-0线程业务处理完毕 运行代码,貌似永远都看不到str的值是"hello Word",而每次都是null,原因很简单的,因为在thread中的run方法中对str进行赋值操作前休眠了1秒,此时main线程中的System.out.println方法已经执行了,所以很难看到str的值是"hello Word",为了看到str的值是"hello Word",我们的一个思路就是等thread运行结束之后,我们再执行System.out.println就可以了,这时候join方法的作用就显现出来的,我们把上面的注释代码删除注释,然后运行,不管运行多少次,输出的结果都是“hello Word”,从这个例子中我们就可以看到join方法的作用,它能够调节各个线程之间的运行顺序,从而可以实现同步。 如果修改注释的代码为thread.join(100);则执行结果为:获取str值:null ,因为主线程已经等不及了。join()他让调用线程在当前线程对象上进行等待当线程执行完毕后,被等待的线程会在推出前调用notifyAll()方法通知所有的等待线程继续执行。public final synchronized void join(long millis) throws InterruptedException {long base = System.currentTimeMillis();long now = 0;if (millis < 0) {throw new IllegalArgumentException("timeout value is negative");}if (millis == 0) {while (isAlive()) {wait(0);}} else {while (isAlive()) {long delay = millis - now;if (delay <= 0) {break;}wait(delay);now = System.currentTimeMillis() - base;}}}本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/135360.htm