Quiero ejecutar algunas tareas diferentes en paralelo, pero tengo el concepto de que si una tarea ya está en cola o se está procesando actualmente, no se volverá a poner en cola. He leído un poco sobre la API de Java y he encontrado el siguiente código, que parece funcionar. ¿Alguien puede arrojar luz sobre si el método que estoy usando es el mejor enfoque? ¿Algún peligro (seguridad de la rosca?) O mejores formas de hacer esto? Código es la siguiente:Thread Pool manejo de tareas 'duplicadas'
import java.util.HashMap;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestExecution implements Runnable {
String key1;
String key2;
static HashMap<TestExecution, Future<?>> executions = new HashMap<TestExecution, Future<?>>();
static LinkedBlockingQueue<Runnable> q = new LinkedBlockingQueue<Runnable>();
static ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 5, 1, TimeUnit.MINUTES, q);
public static void main(String[] args) {
try {
execute(new TestExecution("A", "A"));
execute(new TestExecution("A", "A"));
execute(new TestExecution("B", "B"));
Thread.sleep(8000);
execute(new TestExecution("B", "B"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static boolean execute(TestExecution e) {
System.out.println("Handling "+e.key1+":"+e.key2);
if (executions.containsKey(e)) {
Future<?> f = (Future<?>) executions.get(e);
if (f.isDone()) {
System.out.println("Previous execution has completed");
executions.remove(e);
} else {
System.out.println("Previous execution still running");
return false;
}
}
else {
System.out.println("No previous execution");
}
Future<?> f = tpe.submit(e);
executions.put(e, f);
return true;
}
public TestExecution(String key1, String key2) {
this.key1 = key1;
this.key2 = key2;
}
public boolean equals(Object obj)
{
if (obj instanceof TestExecution)
{
TestExecution t = (TestExecution) obj;
return (key1.equals(t.key1) && key2.equals(t.key2));
}
return false;
}
public int hashCode()
{
return key1.hashCode()+key2.hashCode();
}
public void run() {
try {
System.out.println("Start processing "+key1+":"+key2);
Thread.sleep(4000);
System.out.println("Finish processing "+key1+":"+key2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
seguimiento para comentar a continuación:
El plan es que la activación de las tareas a ejecutar serán manejados por cron llamando al servicio web REST. Por ejemplo, a continuación, se muestra la configuración de una tarea activada a las 9:30 todos los días, más otra programada cada dos minutos.
0/2 * * * * restclient.pl key11 key12
30 09 * * * restclient.pl key21 key22
En este caso, si key11 tarea: key12 se está ejecutando, o ya están en cola para funcionar, no quiero hacer cola otra instancia. Entiendo que tenemos otras opciones para programar, sin embargo, tendemos a utilizar cron para otras tareas, por lo que quiero tratar de mantener esto.
Segunda actualización. En respuesta a los comentarios hasta el momento, he vuelto a escribir el código. ¿Podría comentar algún problema con la siguiente solución actualizada?
import java.util.concurrent.LinkedBlockingQueue;
public class TestExecution implements Runnable {
String key1;
String key2;
static TestThreadPoolExecutor tpe = new TestThreadPoolExecutor(new LinkedBlockingQueue<Runnable>());
public static void main(String[] args) {
try {
tpe.execute(new TestExecution("A", "A"));
tpe.execute(new TestExecution("A", "A"));
tpe.execute(new TestExecution("B", "B"));
Thread.sleep(8000);
tpe.execute(new TestExecution("B", "B"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public TestExecution(String key1, String key2) {
this.key1 = key1;
this.key2 = key2;
}
public boolean equals(Object obj)
{
if (obj instanceof TestExecution)
{
TestExecution t = (TestExecution) obj;
return (key1.equals(t.key1) && key2.equals(t.key2));
}
return false;
}
public int hashCode()
{
return key1.hashCode()+key2.hashCode();
}
public void run() {
try {
System.out.println("Start processing "+key1+":"+key2);
Thread.sleep(4000);
System.out.println("Finish processing "+key1+":"+key2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestThreadPoolExecutor extends ThreadPoolExecutor {
Set<Runnable> executions = Collections.synchronizedSet(new HashSet<Runnable>());
public TestThreadPoolExecutor(LinkedBlockingQueue<Runnable> q) {
super(2, 5, 1, TimeUnit.MINUTES, q);
}
public void execute(Runnable command) {
if (executions.contains(command)) {
System.out.println("Previous execution still running");
return;
}
else {
System.out.println("No previous execution");
}
super.execute(command);
executions.add(command);
}
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
executions.remove(r);
}
}
Por qué no utilizar un hashset para TestExecution en lugar de HashMap ?? –