java多线程join的作用与用法

        对于JAVA的join,JDK 是这样说的:join public final void join (long millis )throws InterruptedException Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever。字面意思是等待一段时间直到这个线程死亡。

        有这样一种应用场景,主线程启动几个子线程分别同时去作一些事情,主线程需等这些子线程都结束后,才能往下执行(如需要获得子线程的结果信息)

        面对这样的场景,我们有几种思路:

        1.启动子线程后,让主线程sleep一段时间

         这种方式最不靠谱,因为我们不知道要让主线程休眠多长时间

        实例1:

package com.bijian.study.thread;

public class MyThread extends Thread {

	boolean negative = true;
	double pi; // Initializes to 0.0, by default

	public void run() {
		for (int i = 3; i < 100000; i += 2) {
			
			if (negative)
				pi -= (1.0 / i);
			else
				pi += (1.0 / i);
			negative = !negative;
		}
		pi += 1.0;
		pi *= 4.0;
		System.out.println("Finished calculating PI");
	}
}

        主线程:

package com.bijian.study.thread;

public class MainThread {

	public static void main(String[] args) {
		MyThread mt = new MyThread();
		mt.start();
		try {
			Thread.sleep(10); // Sleep for 10 milliseconds
		} catch (InterruptedException e) {
		}
		System.out.println("pi = " + mt.pi);
	}
}

         运行结果:

Finished calculating PI
pi = 3.1415726535897894

 

         2.sleep一段时间不靠谱,于是还可以来个循环,判断子线程是否还活着

        实例2(修改实例1的主线程):

package com.bijian.study.thread;

public class MainThread2 {

	public static void main(String[] args) {
		MyThread mt = new MyThread();
		mt.start();
		while (mt.isAlive())
			try {
				Thread.sleep(10); // Sleep for 10 milliseconds
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		System.out.println("pi = " + mt.pi);
	}
}

         运行结果:

Finished calculating PI
pi = 3.1415726535897894

         这样,效果是达到了,但在主线程里写个循环,不是高性能的方法。

 

         3.join很好的解决此应用场景

        实例3(修改实例1的主线程):

package com.bijian.study.thread;

public class MainThread3 {
	public static void main(String[] args) {
		MyThread mt = new MyThread();
		mt.start();
		try {
			mt.join();
		} catch (InterruptedException e) {
		}
		System.out.println("pi = " + mt.pi);
	}
}

         运行结果:

Finished calculating PI
pi = 3.1415726535897894

 

        扩展下实例3,就能很好的解决主线程启动几个子线程分别同时去作一些事情,主线程需等这些子线程都结束后,才能往下执行。如下所示:

          实例4:

          1.新建一个子线程

package com.bijian.study.thread;

public class MyThread2 extends Thread {

	boolean negative = true;
	double res; // Initializes to 0.0, by default

	public void run() {
		for (int i = 1; i < 100; i += 2) {
			if (negative)
				res -= (1.0 / i);
			else
				res += (1.0 / i);
			negative = !negative;
		}
		System.out.println("Finished calculating res");
	}
}

         2.修改实例1的主线程

package com.bijian.study.thread;

public class MainThread3 {
	public static void main(String[] args) {
		
		//创建并启动子线程1
		MyThread mt = new MyThread();
		mt.start();
		
		//创建并启动子线程2
		MyThread2 mt2 = new MyThread2();
		mt2.start();
		try {
			mt.join();
			mt2.join();
		} catch (InterruptedException e) {
		}
		System.out.println("pi = " + mt.pi);
		System.out.println("res = " + mt2.res);
	}
}

         运行结果:

Finished calculating res
Finished calculating PI
pi = 3.1415726535897894
res = -0.7803986631477527

你可能感兴趣的:(java,多线程)