java – Executors没有运行所有线程.
发布时间:2020-12-15 04:16:01 所属栏目:Java 来源:网络整理
导读:我是 Java新手,我正在尝试这个.我有方法,我希望并行运行该方法.我希望有10个线程调用该方法并获得他们的结果. 我正在使用Callable和Executors.我正在创建线程池: ExecutorService executor = Executors.newFixedThreadPool(10); 当我这样做时: executor.in
我是
Java新手,我正在尝试这个.我有方法,我希望并行运行该方法.我希望有10个线程调用该方法并获得他们的结果.
我正在使用Callable和Executors.我正在创建线程池: ExecutorService executor = Executors.newFixedThreadPool(10); 当我这样做时: executor.invokeAll(taskList); 在10个线程中,仅从轮询中获取了1个线程.我只打印了这个: The current thread is pool-1-thread-1 但我希望应该有10个类似的println语句. 这是完整的代码: import java.util.concurrent.Callable; import java.util.List; import java.util.ArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.Future; import java.util.concurrent.ExecutionException; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutorCompletionService; public class Parallel { public static void main(String args[]) { Learning l = new Learning(); l.message = "1st Object"; l.testThread(); } } //this class deals with threads class Learning { public String message; public void setMessage(String message) { this.message = message; } //contains the code where public void testThread() { //create a callable for each method Callable<String> callable1 = new Callable<String>() { @Override public String call() throws Exception { System.out.println("The current thread is " + Thread.currentThread().getName()); return method1(); // return null; } }; //add to a list List<Callable<String>> taskList = new ArrayList<Callable<String>>(); taskList.add(callable1); //create a pool executor with 10 threads ExecutorService executor = Executors.newFixedThreadPool(10); try { List<Future<String>> futureList = executor.invokeAll(taskList); } catch (InterruptedException ie) { } } //put your code here! private String method1() { return Thread.currentThread().getName(); } } 我在这里错过了什么吗? 解决方法
您的ExecutorService可以运行10个线程.但你只提交了一个帖子.将testThread方法更改为这样.
// contains the code where public void testThread() { // add to a list List<Callable<String>> taskList = new ArrayList<Callable<String>>(); Callable<String> callable1=null; for (int i = 0; i < 10; i++) { // create a callable for each method callable1 = new Callable<String>() { @Override public String call() throws Exception { System.out.println("The current thread is " + Thread.currentThread().getName()); return method1(); // return null; } }; taskList.add(callable1); } // create a pool executor with 10 threads ExecutorService executor = Executors.newFixedThreadPool(10); try { List<Future<String>> futureList = executor.invokeAll(taskList); } catch (InterruptedException ie) { } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- java – onUpgrade数据库 – oldVersion – newVersion
- java – 用于检查Selenium WebDriver中项目列表的循环
- 使用Mockito来测试Java的行为
- java – Maven Plugin Build使用Lambdas时失败
- 基于Spring Boot的Logback日志轮转配置详解
- java – 如何获取Foo <?>对象的<?>值?
- Java 8根据条件应用流过滤器
- java – 如何在长时间关闭期间处理servlet请求
- 使用PreparedStatement将一系列Java Enum持久保存到Postgre
- 使用Java 7中的ProcessBuilder构建流程管道