Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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 使用Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())时可能过载;_Java_Multithreading_Memory Management - Fatal编程技术网

Java 使用Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())时可能过载;

Java 使用Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())时可能过载;,java,multithreading,memory-management,Java,Multithreading,Memory Management,我尝试使用多线程调用一个方法超过百万次。因此,我正在使用ExecutorService并传递newFixedThreadPool的动态值: Runtime.getRuntime().availableProcessors() 这将启动数千个线程,这是否会导致处理器负载过重?还是可以一起去 ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); Arra

我尝试使用多线程调用一个方法超过百万次。因此,我正在使用
ExecutorService
并传递
newFixedThreadPool
的动态值:

Runtime.getRuntime().availableProcessors()
这将启动数千个线程,这是否会导致处理器负载过重?还是可以一起去

ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
ArrayList<Future<Boolean>> arrList = new ArrayList<Future<Boolean>>();
    for (final Vlaue val : List) {

        arrList.add(service.submit(new Callable() {
            @Override
            public Boolean call() throws Exception {

                Object obj = client.findObj(
                        val.getItem1(),val.getItem2());
                return obj.isValid();
            }
        }));

    }
    service.shutdown();
    for (Future future : arrList) {
        // at time future returns null here, not sure why?
        assertTrue("Invalid object" future.get());            
    }
ExecutorService service=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
ArrayList arrList=新的ArrayList();
用于(最终版本:列表){
arrList.add(service.submit(new Callable()){
@凌驾
公共布尔调用()引发异常{
对象obj=client.findObj(
val.getItem1(),val.getItem2();
返回obj.isValid();
}
}));
}
service.shutdown();
for(未来:arrList){
//此时future在这里返回null,不知道为什么?
assertTrue(“无效对象”future.get());
}

这将创建
运行时.getRuntime().AvailableProcessor()
线程

无论您向它提交多少任务,当这些线程可用时,它们都将排队并由这些线程处理


因此,换句话说,尽管您对未来的使用似乎有点奇怪,但您所做的一切都很好。很可能有一种更有效的方法可以做到这一点,但您正在做的工作将起作用。

这是启动数千个线程什么?你为什么这么认为?我做了适当的更改,想用future为我打印无效的对象。它如何每次返回几个空值