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

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

 

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");      
      
    }
}

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