Java ThreadFactory:为什么一种使用有效而另一种无效?
Java ThreadFactory: Why does one use of works but other doesn't?
在以下程序中,代码在尝试对方法 second()
中的 Future
执行 get()
时挂起!这是为什么?两个执行程序服务之间的唯一区别是它们使用的 ThreadFactory
。如果我使用 newSingleThreadExecutor
或 newFixedThreadPool
与计数 1 无关紧要。
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);
}
}
您的第一个工厂创建了一个 运行 指定 运行 启用的线程:
Thread t = Executors.defaultThreadFactory().newThread(r);
而在您的第二个工厂中,您只是忘记向创建的线程提供 运行nable:
Thread t = new Thread("tf2-thread");
因此,在您的第二种情况下,运行nable 永远不会 运行,因此 future 永远不会有值。
将第二种情况的线程创建改为
Thread t = new Thread(r, "tf2-thread");
在以下程序中,代码在尝试对方法 second()
中的 Future
执行 get()
时挂起!这是为什么?两个执行程序服务之间的唯一区别是它们使用的 ThreadFactory
。如果我使用 newSingleThreadExecutor
或 newFixedThreadPool
与计数 1 无关紧要。
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);
}
}
您的第一个工厂创建了一个 运行 指定 运行 启用的线程:
Thread t = Executors.defaultThreadFactory().newThread(r);
而在您的第二个工厂中,您只是忘记向创建的线程提供 运行nable:
Thread t = new Thread("tf2-thread");
因此,在您的第二种情况下,运行nable 永远不会 运行,因此 future 永远不会有值。
将第二种情况的线程创建改为
Thread t = new Thread(r, "tf2-thread");