zoukankan      html  css  js  c++  java
  • Callable 与 Future

    package cn.itcast.write;
    
    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;
    
    public class FutureAndCall {
        /**
         * Callable与Runnable类似
         * call方法和run方法
         * 
         * Future 获取结果异常
         */
        public static void main(String[] args) {
            
            //1---------------------------------
            ExecutorService threadPool = Executors.newSingleThreadExecutor();
            Future<String> future = threadPool.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    return "200";
                }
            });
            try {
                System.out.println(future.get());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            threadPool.shutdownNow();
            
            //2---------------------------------
            CallableTask task1 = new CallableTask(1);
            CallableTask task2 = new CallableTask(2);
            CallableTask task3 = new CallableTask(3);
            
            
            try {
                ExecutorService threadPool1= Executors.newFixedThreadPool(3);
                Future<Integer> f1 =  threadPool1.submit(task1);
                System.out.println(f1.get());
                
                Future<Integer> f2 =  threadPool1.submit(task2);
                System.out.println(f2.get());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                f2.cancel(true);
                
                
                Future<Integer> f3 =  threadPool1.submit(task3);
                System.out.println(f3.get());
                
                
                threadPool1.shutdownNow();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (ExecutionException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
            
        }
    }
    
    
    class CallableTask implements Callable<Integer>{
        
        private int data;
        public CallableTask(int data){
            this.data = data;
        }
        
        @Override
        public Integer call() throws Exception {
            
            if (data == 1) {
                return 1;
            }
            
            if (data == 2) {
                while(true){
                    System.out.println("2222");
                    Thread.sleep(1000);
                    return 2;
                }
            }
            
            if (data == 3) {
                /*try {
                    throw new Throwable();
                } catch (Throwable e) {
                    e.printStackTrace();
                }*/
            }
            
            
            return null;
        }
    
        
    }
  • 相关阅读:
    网络基础
    模块和包的介绍与使用
    PHP 接口输出 图片
    Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the
    dedeCMS 两个站共用同一个数据库 图片路径统一
    写入文件_调试方法
    Mysql触发器 使用示例
    部署GitLab遇到的问题记录
    防火墙对nginx服务器有影响
    更新yum源并重建缓存
  • 原文地址:https://www.cnblogs.com/lxh520/p/9337538.html
Copyright © 2011-2022 走看看