我是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();
}
}
我在这里遗漏了什么吗?
答案 0 :(得分:3)
您的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) {
}
}
答案 1 :(得分:2)
taskList
中只有一个callable
。将更多callable
添加到列表中并在method1()
中休眠一段时间,以便池中的所有10个线程都忙碌。
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[10];
for(int i=0;i<10;++i){
callable1[i] = 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>>();
for(int i=0;i<10;++i){
taskList.add(callable1[i]);
}
//create a pool executor with 10 threads
ExecutorService executor = Executors.newFixedThreadPool(10);
try
{
List<Future<String>> futureList = executor.invokeAll(taskList);
executor.shutdown();
}
catch (InterruptedException ie)
{
}
}
//put your code here!
private String method1()
{
return Thread.currentThread().getName();
}
}
答案 2 :(得分:2)
每当有疑问时,请检查javadoc。如果您阅读了invokeAll方法,您将找到以下内容
执行给定的任务,在完成所有任务后返回持有其状态和结果的Futures列表。
因此,如果您提供一项任务,它将完成一项任务,如果您希望完成10项任务,则需要提供10项任务。此外,当没有更多任务要提交时,您可以致电
executor.shutDown()
此方法将在确保完成所有提交的任务后关闭服务。
答案 3 :(得分:0)
问题是执行程序调用可调用或可运行的。在您的任务列表(列表)中,您只添加了一个可调用类型实例,并将此任务列表提交给执行程序。所以遗嘱执行人只能做一份工作。将更多可调用内容添加到任务列表中,然后提交给执行者。