java异步02——多线程

java异步02——多线程

用多线程的方式来实现异步效果。就是在主线程开启一个新线程去处理任务,然后去取这个任务结果。

一个在非异步的例子:

public class AsynchronousByUseThread {
	public static void doSomethingA(){
		try{
			Thread.sleep(2000);
		}catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		System.out.println("--- doSomethingA---");
	}
	public static void doSomethingB(){
		try{
			Thread.sleep(2000);
		}catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		System.out.println("--- doSomethingB---");
	}
	public static void main(String[] args){
		long start = System.currentTimeMillis();
		doSomethingA();
		doSomethingB();
		System.out.println(System.currentTimeMillis()-start);
	}
}

显式创建线程

手段创建一个线程,把方法在线程里执行

public class AsynchronousByUseThread {
	public static void doSomethingA(){
		try{
			Thread.sleep(2000);
		}catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		System.out.println("--- doSomethingA---");
	}
	public static void doSomethingB(){
		try{
			Thread.sleep(2000);
		}catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		System.out.println("--- doSomethingB---");
	}
	public static void main(String[] args){
		long start = System.currentTimeMillis();
		Thread thread = new Thread(()->{	//把方法放在线程里执行
			doSomethingA();
		});
		thread.start();
		doSomethingB();
		try {
			thread.join();	//等待线程里的任务执行完
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(System.currentTimeMillis()-start);
	}
}

线程池

没有返回值的任务

public class AsynchronousByUseThread {	
	private final static int AVALIABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
	private final static ThreadPoolExecutor POOL_EXECUTOR = new ThreadPoolExecutor(AVALIABLE_PROCESSORS, 
			AVALIABLE_PROCESSORS*2, 
			1, 
			TimeUnit.MINUTES, 
			new LinkedBlockingQueue<>(5),
			new ThreadPoolExecutor.CallerRunsPolicy());
	
	public static void doSomethingA(){
		try{
			Thread.sleep(2000);
		}catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		System.out.println("--- doSomethingA---");
	}
	public static void doSomethingB(){
		try{
			Thread.sleep(2000);
		}catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		System.out.println("--- doSomethingB---");
	}
	public static void main(String[] args){
		long start = System.currentTimeMillis();
		POOL_EXECUTOR.execute(()->{
			doSomethingA();
		});
		doSomethingB();
		
		System.out.println(System.currentTimeMillis()-start);
		try {
			Thread.currentThread().join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

有返回值的任务

public class AsynchronousByUseThread {	
	private final static int AVALIABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
	private final static ThreadPoolExecutor POOL_EXECUTOR = new ThreadPoolExecutor(AVALIABLE_PROCESSORS, 
			AVALIABLE_PROCESSORS*2, 
			1, 
			TimeUnit.MINUTES, 
			new LinkedBlockingQueue<>(5),
			new ThreadPoolExecutor.CallerRunsPolicy());
	
	public static String doSomethingA(){
		try{
			Thread.sleep(2000);
		}catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		System.out.println("--- doSomethingA---");
		return "A Task Done";
	}
	public static void main(String[] args){
		Future<String> result = POOL_EXECUTOR.submit(()->doSomethingA());
		try {
			System.out.println(result.get());
		} catch (InterruptedException | ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值