|
|
|
|
Strumenti |
28-04-2006, 22:05 | #1 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
[Java] Threads e wait()
Ragazzi sto studiando un pò i thread in java, in particolare la loro sincronizzazione. Quello che voglio è creare 2 threads che eseguono una operazione per uno, una dietro l'altra.
Io pensavo di eseguire un thread metterlo in wait e svegliare l'altro. Ho creato queste 3 classi: Codice:
public class oggetti { boolean read_state = false; boolean write_state = false; public synchronized void read() throws InterruptedException { System.out.println("Lettore:"); notifyAll(); while(read_state != true) wait(); } public synchronized void write() throws InterruptedException { System.out.println("invio:"); notifyAll(); while(write_state != true) wait(); } public void setReadState(boolean s){ read_state = s; } public void setWriteState(boolean s){ write_state = s; } } Codice:
public class thread_reader implements Runnable { public void run() { oggetti o = new oggetti(); try { while (true) { o.read(); o.setWriteState(true); o.setReadState(false); } } catch (InterruptedException e) { } } } Codice:
public class thread_writer implements Runnable { public void run() { oggetti o = new oggetti(); try { while (true) { o.write(); o.setWriteState(false); o.setReadState(true); } } catch (InterruptedException e) { } } } |
29-04-2006, 19:45 | #2 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
UP
|
29-04-2006, 20:24 | #3 |
Senior Member
Iscritto dal: May 2005
Città: Roma
Messaggi: 7929
|
il notify fallo dopo.....
__________________
My gaming placement |
30-04-2006, 03:05 | #4 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
avevo già provato ma non cambia niente, non va lo stesso
|
30-04-2006, 11:06 | #5 |
Senior Member
Iscritto dal: May 2005
Città: Roma
Messaggi: 7929
|
aspetta che adesso vedo megklio
__________________
My gaming placement |
30-04-2006, 11:07 | #6 |
Senior Member
Iscritto dal: Oct 2000
Messaggi: 235
|
Ma come lanci il programma? riesci a postare il codice che avvia i thread?
Non mi torna il fatto che di istanze di oggetti ne hai 2, una per thread. Mi sembra normale che non succeda niente ed i due thread non comunichino dato che usano due lock diversi.
__________________
...writing about climbing is boring. I would rather go climbing. (Chuck Pratt) |
30-04-2006, 12:15 | #7 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
si hai ragione, infatti poi ho corretto, ma non funziona lo stesso . Posto il codice corretto e anche il main..
Codice:
public class oggetti { boolean read_state = false; boolean write_state = false; public synchronized void read() throws InterruptedException { System.out.println("Lettore:"); while(read_state != true) wait(); notifyAll(); } public synchronized void write() throws InterruptedException { System.out.println("invio:"); while(write_state != true) wait(); notifyAll(); } public void setReadState(boolean s){ read_state = s; } public void setWriteState(boolean s){ write_state = s; } public synchronized void wwait() throws InterruptedException { wait(); } } Codice:
public class thread_reader implements Runnable { oggetti o; public void run() { try { while (true) { o.read(); o.setWriteState(true); o.setReadState(false); } } catch (InterruptedException e) { } } public void setO(oggetti o){ this.o = o; } } Codice:
public class thread_writer implements Runnable { oggetti o; public void run() { try { while (true) { o.write(); o.setWriteState(false); o.setReadState(true); } } catch (InterruptedException e) { } } public void setO(oggetti o) { this.o = o; } } Codice:
public class ProvaThread { public static void main(String[] argv) { thread_reader r = new thread_reader(); thread_writer w = new thread_writer(); oggetti o = new oggetti(); r.setO(o); w.setO(o); Thread re = new Thread(r); Thread wr = new Thread(w); re.start(); wr.start(); } } |
30-04-2006, 12:38 | #8 |
Senior Member
Iscritto dal: Oct 2000
Messaggi: 235
|
Visto il codice mi smebra giusto che non succeda niente:
Quando lanci i thread, entrambi vanno in wait, sullo stesso lock. Non ci sarà mai alcun notify in queste condizioni. Come mai devi metterti in wait anche sul write? fai una prova di questo tipo: - Mantieni il wait sul read, e quando si riattiva satmpi il valore scritto - Il write non fa alcuna wait, ma dopo aver scritto fa notify per risvegliare chi legge Ciao
__________________
...writing about climbing is boring. I would rather go climbing. (Chuck Pratt) |
30-04-2006, 14:05 | #9 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
il write fa una wait perchè deve aspettare che read venga eseguito e a sua volta read fa una wait perchè deve aspettare che write venga eseguito.
Se io tolgo la wait sul write il write viene eseguito molte più volte di quante viene eseguito read. |
30-04-2006, 20:10 | #10 |
Junior Member
Iscritto dal: Apr 2006
Città: Crotone
Messaggi: 13
|
Se le due variabili sono a false e nessuno le setta a true, come fanno i 2 thread ad uscire dal while (dato che entrambi settano a true le rispettive variabili solo dopo aver letto o scritto)? Credo che sia questo il problema...
|
30-04-2006, 21:18 | #11 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
Infatti me ne ero accorto e ho messo read a true visto che è il primo che deve partire.. ma non va lo stesso!
|
04-05-2006, 23:52 | #12 |
Member
Iscritto dal: Feb 2005
Città: Milano
Messaggi: 35
|
Utilizzerei una sola variabile di stato
Codice:
boolean isReading
__________________
Res ipsa loquitur Ultima modifica di FedeX_65246X : 04-05-2006 alle 23:53. Motivo: typo |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 03:51.