jdk8异步

 

 

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;
 
/**
 * 基于jdk1.8实现任务异步处理 *
 */
public class JavaPromise {
    public static void main(String[] args) throws Throwable, ExecutionException {
        // 两个线程的线程池
        ExecutorService executor = Executors.newFixedThreadPool(2);
        //jdk1.8方式写法
        executor.submit(()->{
            try {
                Thread.sleep(1000);
                System.err.println(444);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                executor.shutdown();
            }
        });

        System.out.println("main thread is running");
    }
}

 

posted @ 2021-03-29 19:08  君子笑而不语  阅读(312)  评论(0编辑  收藏  举报