一、如何实现子线程执行完毕再执行主线程?
很多时候在代码的实现过程中,我们想子线程全部运行完成后,再运行主线程后面的流程,如何进行,请看下面两种方法
先看没有处理的情况如下:
public class DoRunnable {
public static void thread() throws InterruptedException{
for(int i = 0; i < 10; i++) {
Thread t2 = new Thread(new Runnable() {
public void run() {
System.out.println("<<<<<<<<<<<<"+ "function1-2:" + Thread.currentThread().getName() + "<<<<<<<<<<<"+ System.currentTimeMillis()+"" +"<<<<<<<<<<<");
countDownLatch.countDown();
}
});
t2.start();
}
}
public static void main(String[] args) throws InterruptedException {
thread();
System.out.println("*******************************end*******************************");
}
}
结果:
<<<<<<<<<<<<function1-2:Thread-2<<<<<<<<<<<1565261950647<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-3<<<<<<<<<<<1565261950647<<<<<<<<<<<
*******************************end*******************************
<<<<<<<<<<<<function1-2:Thread-1<<<<<<<<<<<1565261950647<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-0<<<<<<<<<<<1565261950647<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-7<<<<<<<<<<<1565261950648<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-6<<<<<<<<<<<1565261950648<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-4<<<<<<<<<<<1565261950648<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-5<<<<<<<<<<<1565261950648<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-8<<<<<<<<<<<1565261950649<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-9<<<<<<<<<<<1565261950649<<<<<<<<<<<
1.Join
public class DoRunnable {
public static void thread() throws InterruptedException{
for(int i = 0; i < 10; i++) {
Thread t2 = new Thread(new Runnable() {
public void run() {
System.out.println("<<<<<<<<<<<<"+ "function1-2:" + Thread.currentThread().getName() + "<<<<<<<<<<<"+ System.currentTimeMillis()+"" +"<<<<<<<<<<<");
}
});
t2.start();
t2.join();
}
}
public static void main(String[] args) throws InterruptedException {
thread();
System.out.println("*******************************end*******************************");
}
}
结果:
<<<<<<<<<<<<function1-2:Thread-0<<<<<<<<<<<1565262169575<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-1<<<<<<<<<<<1565262169576<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-2<<<<<<<<<<<1565262169576<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-3<<<<<<<<<<<1565262169576<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-4<<<<<<<<<<<1565262169577<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-5<<<<<<<<<<<1565262169577<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-6<<<<<<<<<<<1565262169577<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-7<<<<<<<<<<<1565262169578<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-8<<<<<<<<<<<1565262169578<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-9<<<<<<<<<<<1565262169578<<<<<<<<<<<
*******************************end*******************************
2.CountDownLatch
public class DoRunnable {
public static void thread() throws InterruptedException{
CountDownLatch countDownLatch = new CountDownLatch(10);
for(int i = 0; i < 10; i++) {
Thread t2 = new Thread(new Runnable() {
public void run() {
System.out.println("<<<<<<<<<<<<"+ "function1-2:" + Thread.currentThread().getName() + "<<<<<<<<<<<"+ System.currentTimeMillis()+"" +"<<<<<<<<<<<");
countDownLatch.countDown();
}
});
t2.start();
}
countDownLatch.await();
}
public static void main(String[] args) throws InterruptedException {
thread();
System.out.println("*******************************end*******************************");
}
}
结果:
<<<<<<<<<<<<function1-2:Thread-0<<<<<<<<<<<1565262078875<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-3<<<<<<<<<<<1565262078875<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-1<<<<<<<<<<<1565262078875<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-2<<<<<<<<<<<1565262078875<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-4<<<<<<<<<<<1565262078876<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-5<<<<<<<<<<<1565262078876<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-6<<<<<<<<<<<1565262078876<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-7<<<<<<<<<<<1565262078876<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-9<<<<<<<<<<<1565262078876<<<<<<<<<<<
<<<<<<<<<<<<function1-2:Thread-8<<<<<<<<<<<1565262078876<<<<<<<<<<<
*******************************end*******************************
总结:
没处理之前,主线程和子线程混合进行。子线程全部运行完成后,再运行主线程后面的流程有两种方法:join和countdownlatch。
join方法实现在t.start之后。阻止主线程活动
countdownlatch方法,需要先实例化,入参带上子线程个数,然后run()执行体中实现countDownLatch.countDown()减1操作,最后在所有子线程循环体外进行countDownLatch.await()。靠计数器保证主线程活动
二、资源不够导致线程阻塞,如何提高线程安全性?
时间有限,可参考线程池 ExecutorService详解,具体内容后续补充。。。
三、线程状态之间的各种转换是如何实现的?
四、如何获取子线程返回值?
方法一:callable和futuretask的线程实现方法,具体详见https://blog.csdn.net/zhouping19851013/article/details/98764627
方法二:子线程run方法中加入存数据库操作,主线程加入延时等待Thread.sleep(time)读取数据库
方法三:线程池 ExecutorService 暂不介绍,详细介绍后续补充