gpt4 book ai didi

java - callable 是否按顺序执行?

转载 作者:行者123 更新时间:2023-12-03 22:10:18 29 4
gpt4 key购买 nike

每当我运行实现可调用的程序时,我都会以顺序形式获得输出。

就像,这是我的程序:

package com.handson;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class WorkSheet_1 implements Callable<String> {

/**
* @param args
*/
private int id;
static int count = 0;
public static String test[] = { "a1" , "a2" , "a3" , "a4" , "a5" , "a6" , "a7" , "a8" ,
"b1" , "b2" , "b3" , "b4" , "b5" , "b6" , "b7" , "b8" ,
"c1" , "c2" , "c3" , "c4" , "c5" , "c6" , "c7" , "c8" ,
"d1" , "d2" , "d3" , "d4" , "d5" , "d6" , "d7" , "d8" ,
"e1" , "e2" , "e3" , "e4" , "e5" , "e6" , "e7" , "e8" ,
"f1" , "f2" , "f3" , "f4" , "f5" , "f6" , "f7" , "f8" ,
"g1" , "g2" , "g3" , "g4" , "g5" , "g6" , "g7" , "g8" ,
"h1" , "h2" , "h3" , "h4" , "h5" , "h6" , "h7" , "h8"};
public WorkSheet_1(int id){
this.id = id;
}

public static void main(String[] args) {
try{
// TODO Auto-generated method stub
BlockingQueue blockingQueue = new ArrayBlockingQueue<WorkSheet_1>(48);
ThreadPoolExecutor testExecutor = new ThreadPoolExecutor(6, 10, 1, TimeUnit.SECONDS, blockingQueue);
for(int i = 0 ;i < test.length ;i++){
Future<String> testFuture = testExecutor.submit(new WorkSheet_1(i));
try {
System.out.println("Output Returned is : "+testFuture.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}catch(RejectedExecutionException e){
e.printStackTrace();
}
}

@Override
public String call() throws Exception {
// TODO Auto-generated method stub
return "Called "+test[this.id];
}



}

可调用输出:
Output Returned is : Called a1
Output Returned is : Called a2
Output Returned is : Called a3
Output Returned is : Called a4
Output Returned is : Called a5
Output Returned is : Called a6
Output Returned is : Called a7
Output Returned is : Called a8
Output Returned is : Called b1
Output Returned is : Called b2
Output Returned is : Called b3
Output Returned is : Called b4
Output Returned is : Called b5
Output Returned is : Called b6
Output Returned is ...............

输出总是按顺序打印数组,而当我实现可运行输出时,输出顺序是任意的:

可运行的输出:
Output Returned is : Called a1
Output Returned is : Called a3
Output Returned is : Called a7
Output Returned is : Called a8
Output Returned is : Called b1
Output Returned is : Called b2
Output Returned is : Called b3
Output Returned is : Called b4 ..............

Why such a difference ?

最佳答案

因为你在 for 循环中等待 callable 的结果:

  System.out.println("Output Returned is : "+testFuture.get());

Future.get() 方法将阻塞,直到结果可用。因此,只有在上一个结果可用后才提交下一个 Callable。

关于java - callable 是否按顺序执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18398379/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com