Future Callable

来源:互联网 发布:hiv 知乎 编辑:程序博客网 时间:2024/06/01 19:59

  public void testFuture() {
        Callable call = new Callable() {
            @Override
            public Object call() throws Exception {
                synchronized (this) {
                    wait(10000l);
                }
                return new Random().nextInt(1000);
            }
        };

        FutureTask future = new FutureTask(call);
        new Thread(future).start();

        try {
            Thread.sleep(1000);
            System.out.println("做完了,等future");
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

0 0
原创粉丝点击