ThreadPoolExecutor con ArrayBlockingQueue
Comencé a leer más sobre ThreadPoolExecutor de Java Doc cuando lo uso en uno de mis proyectos. Entonces, ¿puede alguien explicarme qué significa realmente esta línea? - Sé lo que significa cada parámetro, pero quería entenderlo de una manera más general / laica de algunos de los expertos aquí.
ExecutorService service = new ThreadPoolExecutor(10, 10, 1000L,
TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10, true), new
ThreadPoolExecutor.CallerRunsPolicy());
Actualizado:- La declaración del problema es: -
Cada hilo usa una ID única entre 1 y 1000 y el programa debe ejecutarse durante 60 minutos o más, de modo que en esos 60 minutos es posible que todas las ID se terminen, por lo que debo volver a utilizar esas ID. Así que este es el programa que escribí usando el ejecutor anterior.
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);
}
}
Mis preguntas son: - ¿Este código es correcto en la medida en que se considera o no el rendimiento? ¿Y qué más puedo hacer aquí para hacerlo más preciso? Cualquier ayuda será apreciada.