首页 > 解决方案 > 为什么newsinglethreadexcutor 会阻塞currentthread?

问题描述

我试图理解为什么newsinglethreadexecutor使用可调用期货的get()方法会阻止binderthread其初始化的方法。我怎样才能让它们一起运行?

public class main  {

    public static void main(String[] args) {
        final process prc = new process();

        prc.run();

        System.out.println("main thread" + Thread.currentThread().getName());

    }


};

,,

import java.util.concurrent.*;

public class process  {
    ExecutorService es = Executors.newSingleThreadExecutor();

    public void run() {

        Future fut = es.submit(new Callable<String>() {
            public String call() throws Exception {
                for (byte b : new byte[]{1,2,3,4,4}){
                    System.out.println(b+Thread.currentThread().getName());
                    Thread.sleep(2000);
                }

                return "aa";
            }
        });


        try {
         //   System.out.println(fut.get(1900L, TimeUnit.MILLISECONDS));
            System.out.println(fut.get());
        }catch (Exception a){}
    }


}

标签: javamultithreading

解决方案


推荐阅读