重複の可能性:
forループの最後のカウンター?
以下は私が現在実行しているコードです。イテレータiを新しいRunnable()コードに渡せるようにしたいと思います。私は通常、内部クラスに渡すためにmake変数をfinalにする必要があることを知っていますが、それはイテレーターの効果を台無しにします。通常、Runnable内部クラスを真のクラスにしようと試みることができることは知っていますが(用語が不足しているため)、CountDownLatchesの効果が失われます。
private long generateThreads(int cores){
if (cores <= 0)
cores = Runtime.getRuntime().availableProcessors();
System.out.println("Using " + cores + " threads for processing");
final ExecutorService exec = Executors.newFixedThreadPool(cores);
final CountDownLatch ready = new CountDownLatch(cores);
final CountDownLatch start = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(cores);
for (int i = 1; i <= cores; i++){
exec.execute(new Runnable(){
public void run(){
ready.countDown();
try{
start.await();
//do work
}
} catch (InterruptedException e){
Thread.currentThread().interrupt();
} finally{
done.countDown();
exec.shutdown();
}
}
});
}
long startTime = 0;
try{
ready.await();
startTime = System.nanoTime();
start.countDown();
done.await();
} catch (InterruptedException e){
e.printStackTrace();
}
return System.nanoTime() - startTime;
}
各スレッドにイテレータを与える方法、またはすべての外部クラスメンバー、特にCountDownLatchesを表示するクラスを作成する方法を知っている人はいますか?