Java – executors do not run all threads
•
Java
I'm new to Java and I'm trying this I have a method that I want to run in parallel I want 10 threads to call this method and get their results
I'm using callable and executors I'm creating a thread pool:
ExecutorService executor = Executors.newFixedThreadPool(10);
When I do this:
executor.invokeAll(taskList);
Of the 10 threads, only one thread was obtained from polling I only printed this:
The current thread is pool-1-thread-1
But I hope there should be 10 similar println statements
This is the complete code:
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(); } }
Did I miss anything here?
Solution
Your executorservice can run 10 threads But you only submitted one post Change the testthread method to this
// 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) { } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码