简体   繁体   English

Java ThreadFactory:为什么一个使用作品但其他不使用?

[英]Java ThreadFactory: Why does one use of works but other doesn't?

In the following program the code hangs while trying to do get() on the Future in the method second() ! 在下面的程序中,代码在尝试在方法second()Future执行get()时挂起! Why is that? 这是为什么? The only difference between the two executor services are the ThreadFactory they use. 两个执行程序服务之间的唯一区别是它们使用的ThreadFactory It doesn't matter if I use newSingleThreadExecutor or newFixedThreadPool with count of 1. 如果我使用newSingleThreadExecutornewFixedThreadPool并且计数为1则newFixedThreadPool

package me.test;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;

public class ExecutorServiceTest {
    ThreadFactory tf1 = new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = Executors.defaultThreadFactory().newThread(r);
            t.setDaemon(true);
            t.setName("tf1-thread");
            return t;
        }
    };
    ThreadFactory tf2 = new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread("tf2-thread");
            t.setDaemon(true);
            return t;
        }
    };
    ExecutorService service1 = Executors.newSingleThreadExecutor(tf1);
    ExecutorService service2 = Executors.newSingleThreadExecutor(tf2);
    Callable<Integer> callable = new Callable<Integer>() {
        @Override
        public Integer call() throws Exception {
            return 0;
        }
    };

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorServiceTest executorTest = new ExecutorServiceTest();
        executorTest.first(); // this returns
        executorTest.second(); // this hangs
        System.exit(0);
    }

    void first() throws ExecutionException, InterruptedException {
        Future<Integer> future = service1.submit(callable);
        int result = future.get();
        System.out.println("result=" + result);
    }
    void second() throws ExecutionException, InterruptedException {
        Future<Integer> future = service2.submit(callable);
        int result = future.get();
        System.out.println("result=" + result);
    }
}

Your first factory creates a thread that runs the specified runnable: 您的第一个工厂创建一个运行指定的runnable的线程:

Thread t = Executors.defaultThreadFactory().newThread(r);

Whereas in your second factory you simply forgot to provide the runnable to the created thread: 而在你的第二个工厂,你只是忘了为创建的线程提供runnable:

Thread t = new Thread("tf2-thread");

So, in your second case, the runnable is never run, and so the future never gets a value. 因此,在第二种情况下,runnable永远不会运行,因此未来永远不会获得价值。

Change the thread creation in the second case to 将第二种情况下的线程创建更改为

Thread t = new Thread(r, "tf2-thread");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中使用ThreadFactory - Use of ThreadFactory in java 为什么一个循环抛出一个ConcurrentModificationException,而另一个不抛出? - Why does one loop throw a ConcurrentModificationException, while the other doesn't? Java:ThreadFactory如何处理线程? - Java: How does ThreadFactory handles Threads? 我有以下两种不同的方式在 Java 中声明一个 integer 数组,一种有效,一种无效,为什么? - I have the following two diffetent ways of declaring an integer array in Java, one works and one doesn't, why? 为什么 +1 在这里有效,而 ++ 运算符却没有 - Why does +1 works here but ++ operator doesn't 两个完全相同的应用程序,但一个有效,另一个无效 - Two exact same apps, but one works, the other one doesn´t JMockit - 期望与MockUp <T> 为什么一个工作而另一个不工作? - JMockit - Expectations vs MockUp<T> Why does one work and the other doesn't? 为什么 stream() 在 Scala 中不像在 Java 中那样工作? 是否有其他与 stream() API 相同的 API? - Why doesn't stream() work in Scala the way it does in Java? Is there any other API that does the same as stream() API? 检查密钥是否在地图中-一种方法有效,另一种无效 - Check if key is in map - one way works and the other doesn't 在参数正确的情况下,两个构造函数中的一个起作用,而另一个则不起作用 - Of the two constructers one works and the other doesn't when the argument is correct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM