prudviラジュ:
ワーカースレッドは、runメソッドでは10秒の重いタスクをここで定義されています
import java.util.Date;
import java.util.Random;
import java.util.concurrent.Callable;
public class WorkerThread implements Callable {
private String command;
private long startTime;
public WorkerThread(String s){
this.command=s;
}
@Override
public Object call() throws Exception {
startTime = System.currentTimeMillis();
System.out.println(new Date()+"::::"+Thread.currentThread().getName()+" Start. Command = "+command);
Random generator = new Random();
Integer randomNumber = generator.nextInt(5);
processCommand();
System.out.println(new Date()+ ":::"+Thread.currentThread().getName()+" End.::"+command+"::"+ (System.currentTimeMillis()-startTime));
return randomNumber+"::"+this.command;
}
private void processCommand() {
try {
Thread.sleep(10000);
}
catch (Exception e) {
System.out.println("Interrupted::;Process Command:::"+this.command);
}
}
@Override
public String toString(){
return this.command;
}
}
将来で定義された私のワーカプールは、1秒のタイムアウトを取得します。
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class WorkerPool {
static BlockingQueue queue=new LinkedBlockingQueue(2);
static RejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl();
static ThreadFactory threadFactory = Executors.defaultThreadFactory();
static ThreadPoolExecutor executorPool = new ThreadPoolExecutor(4, 4, 11, TimeUnit.SECONDS, queue, threadFactory, rejectionHandler);
static MyMonitorThread monitor = new MyMonitorThread(executorPool, 3);
public static void main(String args[]) throws InterruptedException, TimeoutException{
List<Future<Integer>> list = new ArrayList<Future<Integer>>();
for(int i=1; i< 5; i++){
WorkerThread worker = new WorkerThread("WorkerThread:::_"+i);
Future<Integer> future = executorPool.submit(worker);
list.add(future);
}
for(Future<Integer> future : list){
try {
try {
future.get(1000, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
future.cancel(true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
executorPool.shutdown();
}
}
スレッドのタイムアウトが将来スレッドのためincresaing維持され、私の期待は中にすべてのスレッドが1秒以上を服用している場合は一度にすべてを閉じる必要があることをする必要があります!二。
aboceのシナリオでは、ワーカースレッドは、プロセスに10秒を取っている、しかし、1秒内でのすべての私の4つのスレッドが、各スレッドのタイムアウトイムタイミングは、タスクごとに1秒でincrementaly増加します。
最初のタイムアウトが1秒秒タイムアウトは2第3番目のタイムアウトが3秒であるthred thredあるthred。
なぜ、すべてのスレッドが1セカンド自体にinteruptingされていませんか?私のコードに問題が?
アダムKotwasinsは次のとおりです。
あなたは、このセクションのループで順番を待っているので。
for(Future<Integer> future : list) {
...
future.get(1000, TimeUnit.MILLISECONDS);
...
}
基本的流れは以下のとおりです。
- all workers 1 .. 4 start
- you wait for worker A to finish
- 1 second passes, TimeoutException (worker A was alive for 1 second)
- you wait for worker B to finish
- 1 second passes, TimeoutException (worker B was alive for 2 seconds)
- you wait for worker C to finish
- 1 second passes, TimeoutException (worker C was alive for 3 seconds)
- ... same for D ...
あなたがのために最大で1秒間待つようにしたい場合は、すべての労働者、あなたがこれまでに待機に費やさどのくらいの時間をカウントし、その後、残り時間を待つ必要があります。擬似コードのような何か:
long quota = 1000
for (Future future : futures) {
long start = System.currentTimeMillis
try {
future.get(quota, MILLISECONDS)
}
catch (TimeoutException e) {
future.cancel(true)
}
finally {
long spent = System.currentTimeMillis() - start
quota -= spent
if (quota < 0) {quota = 0} // the whole block is going to execute longer than .get() only
}
}