zoukankan      html  css  js  c++  java
  • Java8-Executors-No.02

    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.TimeoutException;
    
    public class Executors2 {
    
        public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
    //        test1();
    //        test2();
            test3();
        }
    
        private static void test3() throws InterruptedException, ExecutionException, TimeoutException {
            ExecutorService executor = Executors.newFixedThreadPool(1);
    
            Future<Integer> future = executor.submit(() -> {
                try {
                    TimeUnit.SECONDS.sleep(2);
                    return 123;
                }
                catch (InterruptedException e) {
                    throw new IllegalStateException("task interrupted", e);
                }
            });
    
            future.get(1, TimeUnit.SECONDS);
        }
    
        private static void test2() throws InterruptedException, ExecutionException {
            ExecutorService executor = Executors.newFixedThreadPool(1);
    
            Future<Integer> future = executor.submit(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                    return 123;
                }
                catch (InterruptedException e) {
                    throw new IllegalStateException("task interrupted", e);
                }
            });
    
            executor.shutdownNow();
            future.get();
        }
    
        private static void test1() throws InterruptedException, ExecutionException {
            ExecutorService executor = Executors.newFixedThreadPool(1);
    
            Future<Integer> future = executor.submit(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                    return 123;
                }
                catch (InterruptedException e) {
                    throw new IllegalStateException("task interrupted", e);
                }
            });
    
            System.out.println("future done: " + future.isDone());
    
            Integer result = future.get();
    
            System.out.println("future done: " + future.isDone());
            System.out.print("result: " + result);
    
            executor.shutdownNow();
        }
    
    }
    
  • 相关阅读:
    Hive join操作优化
    php中 date 函数中的格式参数
    Mysql分表之后的聚合统计
    使用Elasticsearch-Dump工具复制ES库
    Json Path 语法详解(Java)
    Json Path 语法
    关于qt5.2~qt5.8的下载地址
    RTL8812AU双频无线网卡在ubuntu19和20上的驱动安装
    使用vscode对threejs的本地调试
    ThreeJS中创建文字的几种方法
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10210906.html
Copyright © 2011-2022 走看看