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

  • 相关阅读:
    Golang里边的map变量是什么?
    Golang map的底层实现
    方法和函数的区别
    Golang 对 对象和指针 的理解
    React 部分
    前端开发概述、html、css基础
    服务器核心知识
    常用模块8.7
    2017.8.2迭代器和生成器
    2017.7.18可变/不可变类型,符号运算及其流程控制
  • 原文地址:https://www.cnblogs.com/silyvin/p/12673209.html
Copyright © 2011-2022 走看看