プロジェクトの1つでThreadPoolExecutorを使用しているときに、JavaDocからThreadPoolExecutorについて詳しく読み始めました。それで、誰かがこの行が実際に何を意味するのか私に説明できますか?-各パラメータが何を表すかは知っていますが、ここの専門家の何人かからより一般的/素人の方法でそれを理解したいと思いました。
ExecutorService service = new ThreadPoolExecutor(10, 10, 1000L,
TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10, true), new
ThreadPoolExecutor.CallerRunsPolicy());
更新:- 問題の説明は次のとおりです:-
各スレッドは1から1000までの一意のIDを使用し、プログラムは60分以上実行する必要があるため、その60分ですべてのIDが終了する可能性があるため、それらのIDを再利用する必要があります。これは、上記のエグゼキュータを使用して作成した以下のプログラムです。
class IdPool {
private final LinkedList<Integer> availableExistingIds = new LinkedList<Integer>();
public IdPool() {
for (int i = 1; i <= 1000; i++) {
availableExistingIds.add(i);
}
}
public synchronized Integer getExistingId() {
return availableExistingIds.removeFirst();
}
public synchronized void releaseExistingId(Integer id) {
availableExistingIds.add(id);
}
}
class ThreadNewTask implements Runnable {
private IdPool idPool;
public ThreadNewTask(IdPool idPool) {
this.idPool = idPool;
}
public void run() {
Integer id = idPool.getExistingId();
someMethod(id);
idPool.releaseExistingId(id);
}
// This method needs to be synchronized or not?
private synchronized void someMethod(Integer id) {
System.out.println("Task: " +id);
// and do other calcuations whatever you need to do in your program
}
}
public class TestingPool {
public static void main(String[] args) throws InterruptedException {
int size = 10;
int durationOfRun = 60;
IdPool idPool = new IdPool();
// create thread pool with given size
ExecutorService service = new ThreadPoolExecutor(size, size, 500L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(size), new ThreadPoolExecutor.CallerRunsPolicy());
// queue some tasks
long startTime = System.currentTimeMillis();
long endTime = startTime + (durationOfRun * 60 * 1000L);
// Running it for 60 minutes
while(System.currentTimeMillis() <= endTime) {
service.submit(new ThreadNewTask(idPool));
}
// wait for termination
service.shutdown();
service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
}
}
私の質問は次のとおりです。-このコードは、パフォーマンスが考慮されているかどうかに関しては正しいですか?そして、それをより正確にするために、他に何をここで作ることができますか?どんな助けでもありがたいです。