|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: May 2003
Messaggi: 93
|
[JAVA] risvegliare un solo thread
ciao a tutti
Devo gestire diversi thread che effettuano richieste e un thread che le soddisfa..questo però deve avvenire una richiesta per volta (richiesta -> soddisfatta .. richiesta -> soddisfatta .. etc ..). I vari Simple_thread e il Soddisfa_thread comunicano tramite un oggetto condiviso... Codice:
class Cell {
public int array_contents[];
public int indirizzo;
private boolean available = false;
public synchronized Cell get() {
while (available == false) {
try {
wait();
}
catch(InterruptedException e) {}
}
available = false;
notifyAll();
return this;
}
public synchronized void put(int []pages,int ind) {
while (available == true) {
try {wait();} catch(InterruptedException e) {}
}
array_contents = pages;
indirizzo = ind;
available = true;
notifyAll();
}
}
C'è un modo per risvegliare solo quel processo, magari tramite altre funzioni che non conosco?
__________________
...Time to bring it down again. Don't just call me pessimist. Try and read between the lines... |
|
|
|
|
|
#2 |
|
Member
Iscritto dal: May 2003
Messaggi: 93
|
non c'è nessuno che sa darmi qualche dritta su come risolvere la cosa?
__________________
...Time to bring it down again. Don't just call me pessimist. Try and read between the lines... |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Texas
Messaggi: 1722
|
Invece di usare la notifyAll(), puoi usare la notify() la quale ne risveglia uno solo, a sua scelta, dei processi in attesa sull'oggetto da te definito.
Nel caso invece tu ne voglia svegliare un thread ben preciso, dovrai cambiare il tuo protocollo, magari mettendo ogni thread in attesa su un oggetto diverso, comunicato dall'oggetto client stesso al server che soddisfa le tue richieste High Flying Sottovento
__________________
In God we trust; all others bring data |
|
|
|
|
|
#4 |
|
Member
Iscritto dal: May 2003
Messaggi: 93
|
grazie sottovento, avevo pensato di metter in attesa il Soddisfa_thread su un'oggetto differente, ma non riesco ad implementarlo..per esempio: considerando un oggetto Obj_attesa obj .. e un oggetto Cell cell ; tutti i Simple_thread si mettono in attesa su cell, mentre il Soddisfa_thread si mette in attesa su obj.
non ci sono rischieste --> Soddisfa_thread si mette in attesa su obj richiesta generata --> Simple_thread risveglia Soddisfa_thread..tramite una obj.notify(); richiesta soddisfatta --> Soddisfa_thread esegue una notifyAll su cell per risvegliare tutti i SImple_thread per generare nuove richieste Come potrei implementare il codice partendo da quello che ho riportato sopra? è giusta almeno l'idea di fondo?
__________________
...Time to bring it down again. Don't just call me pessimist. Try and read between the lines... |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Texas
Messaggi: 1722
|
Beh, io metterei ogni client in attesa su un oggetto diverso:
- Il "server" e' fermo in attesa su di un proprio oggetto. Quando viene svegliato, legge cosa e' stata messa di una coda di comandi (un semplice Vector), esegue e si riaddormenta; - Ogni "client", prepara un oggetto da mettere nella coda del server. In questo oggetto ci sono i dati necessari e c'e' l'oggetto sul quale il "server", al termine delle operazioni, eseguira' la notify(). Ovviamente il client mettera' nella coda del server questo nuovo oggetto e svegliera' con una notify() il server. Subito dopo si addormenta nell'attesa che il server completi le operazioni. Il server, avendo ricevuto il riferimento all'oggetto sul quale il client sta dormendo, lo potra' risvegliare. NOTA - ovviamente il metodo di inserimento in coda del server e relativo notify() dovra' essere synchronized. High Flying Sottovento
__________________
In God we trust; all others bring data |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Jul 2002
Messaggi: 4334
|
Se usi java 5 potresti usare una BlockingQueue, ti risparmi un sacco
di complicazioni: http://java.sun.com/j2se/1.5.0/docs/...kingQueue.html Se non fa al tuo caso come non detto. Ciao
__________________
|Java Base| |
|
|
|
|
|
#7 |
|
Member
Iscritto dal: May 2003
Messaggi: 93
|
Interessante questa BlockingQueue, cmq ho risolto in un modo più semplice di quanto pensassi; faccio comunicare i clients direttamente con il server che possiede metodi sinchronized get e put...insomma..fatto
__________________
...Time to bring it down again. Don't just call me pessimist. Try and read between the lines... |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 11:03.











|








