Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ThreadFactory:为什么一个使用works,而另一个不使用';T_Java_Executorservice - Fatal编程技术网

Java ThreadFactory:为什么一个使用works,而另一个不使用';T

Java ThreadFactory:为什么一个使用works,而另一个不使用';T,java,executorservice,Java,Executorservice,在下面的程序中,代码挂起,同时尝试在方法second()中的Future上执行get()!为什么呢?两个executor服务之间的唯一区别是它们使用的ThreadFactory。无论是使用计数为1的newSingleThreadExecutor还是newFixedThreadPool package me.test; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; impo

在下面的程序中,代码挂起,同时尝试在方法
second()
中的
Future
上执行
get()
!为什么呢?两个executor服务之间的唯一区别是它们使用的
ThreadFactory
。无论是使用计数为1的
newSingleThreadExecutor
还是
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);
    }
}
package me.test;
导入java.util.concurrent.Callable;
导入java.util.concurrent.ExecutionException;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.Future;
导入java.util.concurrent.ThreadFactory;
公共类测试{
ThreadFactory tf1=新的ThreadFactory(){
@凌驾
公共线程newThread(可运行的r){
线程t=Executors.defaultThreadFactory().newThread(r);
t、 setDaemon(true);
t、 setName(“tf1线程”);
返回t;
}
};
ThreadFactory tf2=新的ThreadFactory(){
@凌驾
公共线程newThread(可运行的r){
线程t=新线程(“tf2线程”);
t、 setDaemon(true);
返回t;
}
};
ExecutorService service1=Executors.newSingleThreadExecutor(tf1);
ExecutorService service2=Executors.newSingleThreadExecutor(tf2);
Callable Callable=new Callable(){
@凌驾
公共整数调用()引发异常{
返回0;
}
};
公共静态void main(字符串[]args)引发ExecutionException、InterruptedException{
ExecutorServiceTest executorTest=新的ExecutorServiceTest();
executorTest.first();//返回
executorTest.second();//此挂起
系统出口(0);
}
void first()引发ExecutionException,InterruptedException{
Future=service1.提交(可调用);
int result=future.get();
System.out.println(“结果=”+结果);
}
void second()引发ExecutionException,InterruptedException{
Future=service2.提交(可调用);
int result=future.get();
System.out.println(“结果=”+结果);
}
}

您的第一个工厂创建了一个运行指定可运行线程的线程:

Thread t = Executors.defaultThreadFactory().newThread(r);
而在第二个工厂中,您只是忘记为创建的线程提供runnable:

Thread t = new Thread("tf2-thread");
因此,在第二种情况下,runnable永远不会运行,因此未来永远不会得到值

将第二种情况下的线程创建更改为

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