PDA

View Full Version : [java] Priorità


raniero
04-05-2006, 00:09
Ciao a tutti..
nel mio programma ho 4 thread, vorrei implementare un metodo che aumenta di uno la priorità di un singolo thread ogni volta che va in wait().. Come potrei fare?

io ho provato con questo metodo:

public void aumentaPriorita() {


Thread.currentThread().setPriority(Thread.currentThread().getPriority() + 1);
Main.log.writeINF("AUMENTATA PRIORITA' a thread " + Thread.currentThread().getName() + " " + Thread.currentThread().getPriority() + ">");

}

PGI-Bis
04-05-2006, 00:34
Dovrebbe andare con:

...in un blocco sincronizzato
boolean paused = false;
while(condizioneAttesa) {
try {
wait();
paused = true;
} catch(InterruptedException ex) {
//attesa interrotta
}
}
if(paused) {
Thread t = Thread.currentThread();
int priority = t.getPriority();
if(priority < Thread.MAX_PRIORITY) {
t.setPriority(priority + 1);
}
}

Mi sembra che corrisponda al tuo "ogni volta che entra in pausa...".