|
|
|
![]() |
|
Strumenti |
![]() |
#1 | |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
[JAVA][Concurrency API]Supportare la cancellazione di un task
Salve,
ho la neccessità di definire dei task che vengono eseguiti in parallelo tramite una fixed thread pool: Codice:
... ExecutorService fixedPool = Executors.newFixedTrheadPool(N_THREADS); Codice:
private class SetupTASK implements Callable<OpResult<?>> { private final Session SESSION; SetupTASK(Session s) { SESSION = s; } @Override public OpResult<?> call() throws Exception { ... } } Codice:
private void setupSessions() { FutureTask<?>[] taskBattery = new FutureTask<?>[POOL.N_THREADS]; // create tasks "battery" for (int i=0; i<POOL.N_THREADS; i++) { SetupTASK t = new SetupTASK(SESSIONS[i]); taskBattery[i] = new FutureTask<OpResult<?>>(t); } // execute all tasks POOL.submitTasks(taskBattery); // wait until "battery" execution completes OpResult<?> res = null; for (int i=0; i<POOL.N_THREADS; i++) { try { res = (OpResult<?>) taskBattery[i].get(); ... do something with 'res' } catch(ExecutionException ignored1) {} catch(InterruptedException ignored2) {} } } ... // on another scope, definition of POOL.submitTasks method: <E> void submitTasks(FutureTask<E>[] tasks) { for (FutureTask<E> t : tasks) fixedPool.submit(t); } Quote:
In questo caso, perchè la cosa funzioni, devo supportare l'interruzione del thread che esegue il task? Se sì, è corretto supportarlo chiamando in momenti opportuni Thread.interrupted() sul thread che sta eseguendo il task e se si verifica l'interrupt lanciando una InterruptedException? [esempio di implementazione del metodo call() di un task-tipo]: Codice:
@Override public OpResult<?> call() throws Exception { OpResult<?> res = new OpResult<Object>(); checkInterrupt(); res = SESSION.connect(BASE_SESSION); if (res.isError()) { return res; } checkInterrupt(); res = SESSION.login(BASE_SESSION); if (res.isError()) { return res; } String[] dirs = BASE_SESSION.arrayPath(); for (String dir : dirs) { checkInterrupt(); res = SESSION.changeDir(dir); if (res.isError()) { return res; // break loop } } return res; } private void checkInterrupt() throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); } Grazie mille, anche solo per la pazienza di aver letto ![]() P.S.: per via di scelte dell'architettura del progetto SwingWorker non è un'opzione.
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 05-03-2010 alle 13:47. |
|
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
In sintesi sì. Questo:
Codice:
Callable<Void> task = new Callable<Void>() { public Void call() throws Exception { while(true) {} } } Codice:
Callable<Void> task = new Callable<Void>() { public Void call() throws Exception { while(true) { if(Thread.interrupted()) throw new Exception("Lavalachelavaben"); } } }
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
I miei dubbi erano dati dal fatto che FutureTask implementa un metodo 'cancel' e un metodo 'isCancelled'.
Ricordando gli esempi concernenti l'uso del metodo 'doInBackground' di SwingWorker avevo il dubbio fosse sufficiente, anche per quanto riguarda il metodo 'run' di un Callable, controllare internamente con 'isCancelled' se il task fosse stato annullato da una precedente chiamata su 'cancel'. Invece non c'entra niente: isCancelled viene chiamato sempre e solo alla fine della computazione del FutureTask e serve solo per sapere a posteriori se il task ha terminato la computazione perchè cancellato oppure no. Grazie della conferma.
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 20:21.