zoukankan      html  css  js  c++  java
  • java_有返回值线程_提前加载例子

    package com.demo.test3;
    
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;
    
    /**
     * @author QQ: 1236897
     *
     */
    
    //有返回值线程
    //提前加载
    public class FutureTaskTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            long start = System.currentTimeMillis();
            PreLoad preLoad = new PreLoad();
    
            preLoad.start();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ProctInfo proctInfo = null;
            
            try {
                proctInfo = preLoad.get();
            } catch (DataLoadException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
            System.out.println(proctInfo.getId());
            
            long end = System.currentTimeMillis();
            
            System.out.println("Time - " + (end - start));
        }
    
    }
    
    class PreLoad {
    
        private final FutureTask<ProctInfo> future = new FutureTask<ProctInfo>(
                new Callable<ProctInfo>() {
                    @Override
                    public ProctInfo call() throws Exception {
                        // TODO Auto-generated method stub
                        Thread.sleep(6000);
                        return new ProctInfo();
                    }
    
                });
    
        private final Thread thread = new Thread(future);
    
        public void start() {
            thread.start();
        }
    
        public ProctInfo get() throws DataLoadException, InterruptedException {
    
            try {
                return future.get();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                Throwable cause = e.getCause();
                if (cause instanceof DataLoadException) {
                    throw (DataLoadException) cause;
                } else {
                    throw launchThrowable(e);
                }
            }
    
        }
        
        public static RuntimeException launchThrowable(Throwable t){
            
            if(t instanceof RuntimeException){
                return (RuntimeException)t;
            }else if(t instanceof Error){
                throw (Error) t;
                
            }else{
                throw new IllegalStateException("Uncheck exception");
            }
            
        }
    }
    
    class ProctInfo {
        private String id = "111";
    
        /**
         * @return the id
         */
        public String getId() {
            return id;
        }
    
    }
  • 相关阅读:
    python爬虫,scrapy,获取响应的cookie,获取返回的cookie
    this指向
    闭包的10种形式
    nodejs 公私钥文件加密解密
    mysql基础知识
    nodejs 连接mysql 集群,开启事务,事务回滚封装
    pm2 启动eggjs,
    js身份证验证,二代身份证,大陆,权重验证,正规
    nodejs限制IP一段时间内的访问次数
    nodejs链接mysql集群,nodejs PoolCluster : Error: Too many connections
  • 原文地址:https://www.cnblogs.com/MarchThree/p/4769860.html
Copyright © 2011-2022 走看看