Ho un dubbio, ho fatto un semplice programmino in java che esegue 3 Thread e volevo vedere i vari cambiamenti settando la priorità di ciascun thread.
ecco il programmino:
Non capisco perché il risultato è sempre lo stesso, vengono eseguiti in sequenza e non per ordine di priorità. Qualcuno mi spiega perché?public class ThreadSleep {
public static void main( String args[] ) {
PrintThread th1 = new PrintThread( "Thread 1", 6 );
PrintThread th2 = new PrintThread( "Thread 2", 4 );
PrintThread th3 = new PrintThread( "Thread 3", 9 );
System.out.println("Partenza thread");
th1.start();
th2.start();
th3.start();
System.out.println("Thread partiti, fine main");
}
}
class PrintThread extends Thread {
private int v_sleep;
// Costruttore
public PrintThread( String nome, int p ) {
super( nome );
setPriority( p );
}
public void run() {
System.out.println( getName() + ": completato. " + "Priorità: " + getPriority() );
}
}
Perché poi facendo un programma più complicato dove devo impostare delle priorità non vengono rispettate e non mi funziona bene :-(
Cmq ecco l'output del programma precedente, come potete vedere prima viene completato il Thread 1 poi il 2 e poi il 3, ma teoricamente la sequenza dovrebbe essere 3, 1 , 2.
Partenza thread
Thread 1: completato. Priorità: 6
Thread 2: completato. Priorità: 4
Thread partiti, fine main
Thread 3: completato. Priorità: 9![]()
![]()