Java-多线程的几种特殊使用

一、如何实现子线程执行完毕再执行主线程?

很多时候在代码的实现过程中,我们想子线程全部运行完成后,再运行主线程后面的流程,如何进行,请看下面两种方法

先看没有处理的情况如下:

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 暂不介绍,详细介绍后续补充

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值