zoukankan      html  css  js  c++  java
  • 模拟future

    import java.util.concurrent.ExecutionException;
    
    abstract public class MyFuture {
    
        private volatile Object returnVal;
        private volatile Boolean isException = false;
        private volatile ExecutionException exception;
        private Thread thread;
    
        public abstract Object call() throws Exception;
    
        public void start() {
            thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        returnVal = call();
                    } catch (Exception e) {
                        exception = new ExecutionException(e);
                        isException = true;
                    }
                }
            });
            thread.start();
        }
    
        public Object get() throws InterruptedException, ExecutionException {
            thread.join();
            if(isException)
                throw exception;
            return returnVal;
        }
    
    
        public static void main(String [] f) {
    
            Long st = System.currentTimeMillis();
    
            MyFuture myFuture = new MyFuture() {
                @Override
                public Object call() throws Exception {
                    Thread.sleep(5000);
                    return 1;
                }
            };
    
            myFuture.start();
    
            try {
                Object ret = myFuture.get();
                System.out.println(ret);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
    
            System.out.println(System.currentTimeMillis() - st);
    
            MyFuture myFutureEx = new MyFuture() {
                @Override
                public Object call() throws Exception {
                    Thread.sleep(5000);
                    int a = 1/0;
                    return 1;
                }
            };
    
            myFutureEx.start();
    
            try {
                Object ret = myFutureEx.get();
                System.out.println(ret);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
    
            System.out.println(System.currentTimeMillis() - st);
        }
    }
    

      

    1
    5010
    10016
    java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero
    at Thread.MyFuture$1.run(MyFuture.java:24)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.ArithmeticException: / by zero
    at Thread.MyFuture$3.call(MyFuture.java:69)
    at Thread.MyFuture$1.run(MyFuture.java:22)
    ... 1 more

  • 相关阅读:
    Java 处理 XML
    Spring MVC全局异常处理
    ThreadLocal与Synchronized区别
    office xml 方式
    Nginx ssl证书部署方法
    利用nginx来屏蔽指定的user_agent的访问以及根据user_agent做跳转
    Nginx使用GZIP来压缩网页
    Kibana安装与基本用法(ELK)
    用Linux自带的Logrotate来管理日志
    Net accounts命令
  • 原文地址:https://www.cnblogs.com/silyvin/p/12673209.html
Copyright © 2011-2022 走看看