Salve,
Non capisco la successione dell'output del seguente esempio (che ho semplificato per brevità):
Ecco l'output:Codice PHP:public class ThreadTester {
public static void main( String args[] )
{
PrintThread thread1, thread2;
thread1 = new PrintThread( "thread1" );
thread2 = new PrintThread( "thread2" );
System.err.println( "\nStarting threads" );
thread1.start();
thread2.start();
System.err.println( "Threads started\n" );
}
}
class PrintThread extends Thread {
private int sleepTime;
public PrintThread( String name )
{
super( name );
sleepTime = 2;
System.err.println( "Name: " + getName() +
"; sleep: " + sleepTime );
}
public void run()
{
try {
System.err.println( getName() + " going to sleep" );
Thread.sleep( sleepTime );
}
catch ( InterruptedException exception ) {
System.err.println( exception.toString() );
}
System.err.println( getName() + " done sleeping" );
}
}
Perchè la stringa Threads started non sta alla fine?Name: thread1; sleep: 2
Name: thread2; sleep: 2
Starting threads
Threads started
thread1 going to sleep
thread2 going to sleep
thread1 done sleeping
thread2 done sleeping
Grazie dell'attenzione.
Matteo

Rispondi quotando
- se avessi detto che la chiamata al metodo start implica che il thread attuale si trova nello stato di "ready" e quindi pronto ad essere eseguito, ovvero ad eseguire il relativo metodo run.
) ho capito meglio quell'esempio. Devo dire però che mi capita di rileggere più volte lo stesso paragrafo per capire veramente il capitolo sui thread

