我有這個簡單的代碼,它不會節省時間。運行時間與使用單線程相同。哪裏不對?你能幫忙嗎?我對此很陌生,如果它太簡單,我希望這個問題不會讓你煩惱。謝謝!爲什麼我的JAVA線程池不節省時間?
import java.util.Random;
import java.util.concurrent.Callable;
public class Work implements Callable<int[]>
{
int id;
int y;
public Work(int i)
{
id=i;
}
public int[] doit(){
Random rand = new Random();
y=rand.nextInt(10);
try {
Thread.sleep(y*1000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
System.out.println(ex.getMessage());
}
int x[] = new int[2];
x[0]=id;
x[1]=y;
return x;
}
@Override
public int[] call() throws Exception
{
return doit();
}
}
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Driver
{
ArrayList<int[]> result=new ArrayList<int[]>() ;
public Driver()
{
}
public void doCalc(int n)
{
for (int i=0; i<n; i++) {
Work w = new Work(i);
int[] y = w.doit();
if (y != null) {
result.add(y);
}
}
int total=0;
for (int i=0; i< result.size(); i++) {
System.out.println("id = " + result.get(i)[0] + "; random number = "+ result.get(i)[1]);
total+=result.get(i)[1];
}
System.out.println("total = " + total);
}
public void doCalcThread(int n)
{
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i=0; i<n; i++) {
Work w = new Work(i);
Future<int[]> future =executor.submit(w);
int[] y;
try
{
y = future.get();
if (y != null) {
result.add(y);
}
} catch (InterruptedException | ExecutionException e)
{
e.printStackTrace();
}
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
int total=0;
for (int i=0; i< result.size(); i++) {
System.out.println("id = " + result.get(i)[0] + "; random number = "+ result.get(i)[1]);
total+=result.get(i)[1];
}
System.out.println("total = " + total);
}
/**
* @param args
*/
public static void main(String[] args)
{
Driver dr = new Driver();
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
cal = Calendar.getInstance();
System.out.println("before " + sdf.format(cal.getTime()));
//dr.doCalc(5);
dr.doCalcThread(5);
cal = Calendar.getInstance();
System.out.println("after " + sdf.format(cal.getTime()));
}
}
基本上,您希望結果能夠從Future回到現在?那麼你可以開始幾項平行工作並從未來收集他們的結果? –