多任务同时执行完毕后方执行后续任务

多线程同时启动,执行完毕后启动新任务

 

1.传统join方式,等待所有线程执行完毕方才后续执行

public class ThreadTest {
	interface Task extends Runnable {
		Object getResult();
	}

	public static void main(String[] args) throws InterruptedException {
		Task task1, task2, task3;
		task1 = new Task() {
			@Override
			public void run() {
				try {
					Thread.sleep(8000);
					System.out.println("t1 returns " + getResult());
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			@Override
			public Object getResult() {
				return 1;
			}
		};
		task2 = new Task() {
			@Override
			public void run() {
				try {
					Thread.sleep(5000);
					System.out.println("t2 returns " + getResult());
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			@Override
			public Object getResult() {
				return 2;
			}
		};
		task3 = new Task() {
			@Override
			public void run() {
				try {
					Thread.sleep(5000);
					System.out.println("t3 returns " + getResult());
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			@Override
			public Object getResult() {
				return 3;
			}
		};
		Thread t1, t2, t3;
		t1 = new Thread(task1);
		t2 = new Thread(task2);
		t3 = new Thread(task3);
		t1.start();
		t2.start();
		t3.start();
		t1.join();
		t2.join();
		t3.join();
		System.out.println("All three threads have died");
	}

}

 

2.ExecutorService + Callable + Future

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

class CallableTest implements Callable<Integer> {
    
    private final long waitTime;
    private final int taskId;
    
    public CallableTest(long waitTime, int taskId) {
        this.waitTime = waitTime;
        this.taskId = taskId;
    }

    public Integer call() throws Exception {
        System.out.println("Hello, this is task: "+this.taskId);    
        Thread.sleep(this.waitTime);
        return 1;
    }

}

public class Test {

    public static void main(String[] args) {
        
        ExecutorService  executor = Executors.newFixedThreadPool(3);
       
        CallableTest task1 = new CallableTest(10*1000l, 1);
        CallableTest task2 = new CallableTest(8*1000l, 2);
        CallableTest task3 = new CallableTest(5*1000l, 3);
        
        Future<Integer> future1= executor.submit(task1);
        Future<Integer> future2= executor.submit(task2);
        Future<Integer> future3= executor.submit(task3);
        
        while(true){
            if(future1.isDone() && future2.isDone() && future3.isDone()){
                break;
            }
        }
       
        try {
            System.out.println("Return Value from task1: " + future1.get());
            System.out.println("Return Value from task2: " + future2.get());
            System.out.println("Return Value from task3: " + future3.get());            
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        
        System.out.println("All tasks finished");      
      
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值