|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Mar 2007
Città: Milano Beach
Messaggi: 1696
|
[JAVA] Polling su file con thread
Buongiorno a tutti.
![]() Stavo giocando con i thread in java per realizzare un semplice poller che ad intervalli regolari va a verificare se un certo file è presente e, in caso affermativo, lo processa (un banalissimo producer-consumer). Il tutto funziona, ma ho un solo problema... temo che una volta processato il file il thread consumer rimanga in esecuzione (per dire, nella vista Debug di Eclipse non mi appare [Terminated]).. ho iniziato da poco a giocherellare con i thread e non escludo grosse cazzate. ![]() Per il producer ho usato uno ScheduledExecutorService in modo da poter eseguire il check ogni n secondi. Ecco il codice: TestClass.java Codice:
import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class TestClass { boolean isFilePresent = false; String filename; File xmlToRead = null; public TestClass(String filename) { this.filename = filename; } synchronized void checkFilePresence() { System.out.println("[checkFilePresence] IN"); if (isFilePresent) { System.out.println("[checkFilePresence] File già letto."); return; } xmlToRead = new File(filename); if (xmlToRead.exists() == false) { System.out.println("[checkFilePresence] File non ancora presente.."); return; } notify(); System.out.println("[checkFilePresence] File trovato!"); } synchronized void printFileContent(Producer producer) { System.err.println("[printFileContent] IN"); if (!isFilePresent) try { wait(); } catch (InterruptedException exc) { System.out.println("Caught InterruptedException in printFileContent"); } // Processo file isFilePresent = false; producer.stop(); System.err.println("[printFileContent] Out"); } } Producer.java Codice:
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; public class Producer { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); private ScheduledFuture<?> checkerHandle = null; public Producer(final TestClass test) { final Runnable checker = new Runnable() { @Override public void run() { test.checkFilePresence(); } }; checkerHandle = scheduler.scheduleAtFixedRate(checker, 1, 2, TimeUnit.SECONDS); scheduler.schedule(new Runnable() { @Override public void run() { checkerHandle.cancel(true); } }, 60, TimeUnit.SECONDS); } public void stop() { checkerHandle.cancel(true); } } Codice:
public class Consumer implements Runnable { private TestClass test; private Producer producer; public Consumer(TestClass test, Producer producer) { this.test = test; this.producer = producer; Thread thread = new Thread(this, "Consumer"); thread.start(); } public void run() { test.printFileContent(producer); } } Codice:
public class Main { public static void main(String[] args) { TestClass test = new TestClass("/home/gibbo/bla.log"); Producer producer = new Producer(test); new Consumer(test, producer); } } ![]()
__________________
~ Cthulhu: MacBookPro 13.3" ~ Azathoth: D510MO |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Analizzando il codice che hai postato e il sintomo che rilevi (consumer non "muore") ipotizzo che si verifichi un deadlock.
In particolare quando consumer ottiene il lock sull'istanza di TestClass e isFilePresent vale false: consumer va in wait() durante la chiamata a printFileContent e non rilascia il lock sull'istanza. Quindi il producer schedulato rimarrà in attesa di poter acquisire il lock chiamando il metodo checkFilePresence sull'istanza di TestClass, lock che non verrà liberato perchè posseduto da consumer che a sua volta attende che producer lo risvegli dalla wait().
__________________
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) |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Jul 2011
Messaggi: 381
|
Ho dato un'occhiata un po' rapida e mi sembra di notare che c'è la possibilità che il main termini prima dei thread. Non vedo da nessuna parte un join(). Comunque è un codice molto molto molto contorto, non so se per scelta tua o per specifiche.
Generalmente un thread viene implementato in una classe esempio: public class Producer extends Thread { producer(){...} run(){...} } Gli oggetti che devono essere usati concorrentemente non dovrebbero modificare i thread direttamente, ma dovrebbero essere i thread a modificarsi in base all'oggetto. Tra l'altro mi sembra di capire il consumatore utilizzi la checkerHandle.cancel(true); per far terminare il loop del produttore; come prima facendo così aumenti di molto la probabilità di starvation. Comunque non sto dicendo che sia sbagliato, solo che secondo me aumenti di molto la complessità e rischi di commettere parecchi errori. ciao ![]()
__________________
Concluso positivamente con: Kamzata, Ducati82, Arus, TheLastRemnant, ghost driver, alexbull1, DanieleRC5, XatiX Ultima modifica di starfred : 26-09-2011 alle 10:39. |
![]() |
![]() |
![]() |
#4 | |||||
Senior Member
Iscritto dal: Mar 2007
Città: Milano Beach
Messaggi: 1696
|
Quote:
![]() (Può essere che non abbia afferrato chiaramente il funzionamento di wait() e notify(). Vado a ripassarli per sicurezza. ![]() Quote:
![]() Quote:
![]() Quote:
Quote:
![]() Ciao e grazie mille ad entrambi! ![]()
__________________
~ Cthulhu: MacBookPro 13.3" ~ Azathoth: D510MO |
|||||
![]() |
![]() |
![]() |
#5 | |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
Una volta che il thread ha acquisito il lock del monitor dell'istanza, procede a eseguire il corpo del metodo. Quando il thread "esce" dal metodo, rilascia il lock. Producer (il thread dello scheduled executor) e consumer (il thread che istanzia nel suo construttore) operano entrambi sulla stessa istanza di TestClass, la quale ha i metodi syncrhonized. Ora immagina che succeda questo: Codice:
- consumer invoca printFileContent() - consumer acquisisce il lock dell'istanza di TestClass - consumer va in wait perchè isFilePresent vale false - producer invoca checkFilePresence() - producer non riesce ad aquisire il lock dell'istanza di TestClass, perchè posseduto da consumer
__________________
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 : 26-09-2011 alle 11:37. |
|
![]() |
![]() |
![]() |
#6 | |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
La java virtual machine non "muore" finchè ci sono thread di tipo non-demone in esecuzione.
__________________
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) |
|
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Mar 2007
Città: Milano Beach
Messaggi: 1696
|
Ok sono un pirla, nell'analizzare la tua risposta non avevo considerato il fatto che i metodi sono dichiarati synchronized.
![]() Capito ora, grazie mille. ![]()
__________________
~ Cthulhu: MacBookPro 13.3" ~ Azathoth: D510MO Ultima modifica di GByTe87 : 26-09-2011 alle 11:45. |
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Jul 2011
Messaggi: 381
|
Ciao, a grandi linee io avrei fatto
Pseudo-codice Codice:
public class Producer extends Thread { producer(string nomefile){ } run(){ TestClass.controllo(nomefile); } } public class Consumatore extends Thread { Consumatore (){...} run(){ TestClass.leggi(); } } public class TestClass { boolean pronto=false; string file_da_leggere; TestClass (){...} synchronized void controllo(string nomefile){ file_da_leggere=nomefile; //Setto il nome del file da leggere pronto=true; // Memorizzo che è pronto un dato nuovo notify(); //Risveglio un eventuale consumatore in attesa wait(); //Attendo che il consumatore abbia prelevato il dato. } synchronized void leggi(){ if (!pronto) wait(); //Se non è pronto il file da leggere attendi /*Leggi il file_da_leggere */ pronto=false; //Resetto nel caso volessi riutilizzare la TestClass notify(); //Risveglio il produttore } } public class Main { public static void main(String[] args) { TestClass test = new TestClass(); Producer producer = new Producer(test,"/home/gibbo/bla.log"); Consumatore consumat = new Consumatore (test); producer.join(); consumat.join(); } } Io partirei da questa struttura e poi ci costruirei sopra.
__________________
Concluso positivamente con: Kamzata, Ducati82, Arus, TheLastRemnant, ghost driver, alexbull1, DanieleRC5, XatiX |
![]() |
![]() |
![]() |
#9 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
@starfred: ciao, scusa, ma il tuo precedente esempio mi sembra prono a deadlock, proprio come il codice inzialmente postato da GByTe87; controllo() e leggi() sono entrambi metodi synchronized; cosa succede se un thread ottiene il lock sul monitor dell'istanza condivisa di TestClass e poi non lo rilascia perchè entra in wait?
__________________
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) |
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: Jul 2011
Messaggi: 381
|
E' una proprietà intrinseca nella wait(). Quando un thread esegue la wait() rilascia il monitor ed entra in un'altra coda di attesa. Quando viene eseguita la notify il thread risvegliato rientra in coda al monitor.
__________________
Concluso positivamente con: Kamzata, Ducati82, Arus, TheLastRemnant, ghost driver, alexbull1, DanieleRC5, XatiX |
![]() |
![]() |
![]() |
#11 | |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
Meglio ribadirlo a beneficio di GByTe87... chiedo scusa per l'eventuale confusione generata a causa del mio errore.
__________________
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) |
|
![]() |
![]() |
![]() |
#12 |
Senior Member
Iscritto dal: Mar 2007
Città: Milano Beach
Messaggi: 1696
|
Bene, sono riuscito nel mio intento.
![]() Producer.java Codice:
public class Producer extends Thread { private TestClass test; private String filename; public Producer(TestClass test, String filename) { this.test = test; this.filename = filename; } @Override public void run() { boolean end = false; while (end == false) { end = test.check(filename); try { sleep(1000); } catch (InterruptedException exc) { System.err.println(exc.getMessage()); } } } } Codice:
public class Consumer extends Thread { private TestClass test; public Consumer(TestClass test) { this.test = test; } @Override public void run() { boolean end = true; while (end) end = test.read(); } } Codice:
import java.io.File; public class TestClass { String fileName; private boolean isFileAvailable = false; public synchronized boolean read() { System.err.println("readin"); while (isFileAvailable == false) { try { System.err.println("[read] wait() in | isFileAvailable = " + isFileAvailable); wait(); System.err.println("[read] wait() out | isFileAvailable = " + isFileAvailable); } catch (InterruptedException exc) { System.err.println(exc.getMessage()); } } System.err.println("File pronto!"); isFileAvailable = false; notify(); System.err.println("readout"); return isFileAvailable; } public synchronized boolean check(String filename) { System.err.println("checkin"); this.fileName = filename; while (isFileAvailable == true) { try { System.err.println("[check] wait() in | isFileAvailable = " + isFileAvailable); wait(); System.err.println("[check] wait() out | isFileAvailable = " + isFileAvailable); } catch (InterruptedException exc) { System.err.println(exc.getMessage()); } } File file = new File(fileName); if (file.exists()) { isFileAvailable = true; notify(); } System.err.println("checkout: isFileAvailable = " + isFileAvailable); return isFileAvailable; } } ![]() Suggermenti su qualcosa di simile da implementare per essere sicuro di aver capito? ![]() Grazie ad entrambi ![]()
__________________
~ Cthulhu: MacBookPro 13.3" ~ Azathoth: D510MO Ultima modifica di GByTe87 : 26-09-2011 alle 21:58. |
![]() |
![]() |
![]() |
#13 |
Senior Member
Iscritto dal: Jul 2011
Messaggi: 381
|
Ciao, la condizione while (isFileAvailable == true) della boolean check(String filename) non verrà mai eseguita per il fatto che il Producer è l'unico che può settare (isFileAvailable = true) ma quando effettua quella operazione esce dal ciclo while (end == false). Quindi, a meno che tu nel main non hai altri Producer, la condizione sopra citata è inutile.
![]()
__________________
Concluso positivamente con: Kamzata, Ducati82, Arus, TheLastRemnant, ghost driver, alexbull1, DanieleRC5, XatiX |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 04:12.