码迷,mamicode.com
首页 > 编程语言 > 详细

从头认识java-18.2 基本的线程机制(7)join

时间:2016-01-20 13:22:22      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

这一章节我们来讨论一下join。

1.什么是join?

堵塞当前线程,让.join的线程先做完,然后再继续当前线程

下面是api的解释:

A.join,在API中的解释是,堵塞当前线程B,直到A执行完毕并死掉,再执行B。 


2.例子:

package com.ray.ch17;

public class Test {

	public static void main(String[] args) throws InterruptedException {
		System.out.println(Thread.currentThread().getName() + " start");
		SleepThread sleepThread = new SleepThread("sleep");
		Thread.sleep(2000);
		JoinThread joinThread = new JoinThread(sleepThread, "join");
//		joinThread.join();
		System.out.println(Thread.currentThread().getName() + " end");
	}
}

class SleepThread extends Thread {

	public SleepThread(String name) {
		super(name);
		start();
	}

	@Override
	public void run() {
		try {
			for (int i = 0; i < 5; i++) {
				System.out.println(getName() + " SleepThread");
				Thread.sleep(1000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class JoinThread extends Thread {

	private SleepThread sleepThread;

	public JoinThread(SleepThread sleepThread, String name) {
		super(name);
		this.sleepThread = sleepThread;
		start();
	}

	@Override
	public void run() {
		try {
			System.out.println(getName() + " join start");
			sleepThread.join();
			System.out.println(getName() + " join end");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

输出:

main start
sleep SleepThread
sleep SleepThread
sleep SleepThread
main end
join join start
sleep SleepThread
sleep SleepThread
join join end


package com.ray.ch17;

public class Test {

	public static void main(String[] args) throws InterruptedException {
		System.out.println(Thread.currentThread().getName() + " start");
		SleepThread sleepThread = new SleepThread("sleep");
		Thread.sleep(2000);
		JoinThread joinThread = new JoinThread(sleepThread, "join");
		joinThread.join();
		System.out.println(Thread.currentThread().getName() + " end");
	}
}

class SleepThread extends Thread {

	public SleepThread(String name) {
		super(name);
		start();
	}

	@Override
	public void run() {
		try {
			for (int i = 0; i < 5; i++) {
				System.out.println(getName() + " SleepThread");
				Thread.sleep(1000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class JoinThread extends Thread {

	private SleepThread sleepThread;

	public JoinThread(SleepThread sleepThread, String name) {
		super(name);
		this.sleepThread = sleepThread;
		start();
	}

	@Override
	public void run() {
		try {
			System.out.println(getName() + " join start");
			sleepThread.join();
			System.out.println(getName() + " join end");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

输出:

main start
sleep SleepThread
sleep SleepThread
sleep SleepThread
join join start
sleep SleepThread
sleep SleepThread
join join end
main end


上面两个代码的不同主要是体现在joinThread.join();是否被注释。

对比上面的两个代码的输出,主要不同在于main end 的位置,当joinThread.join();的时候,由于需要先阻塞main方法的线程,先完成joinThread所在线程,因此main end的位置会出现上面的变化。

另一方面,我们从sleep和join两个线程输出的文字的位置也可以推导出上面的结论。


总结:这一章节我们简单介绍了一下join。


这一章节就到这里,谢谢。

-----------------------------------

目录


从头认识java-18.2 基本的线程机制(7)join

标签:

原文地址:http://blog.csdn.net/raylee2007/article/details/50547454

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!