Documente Academic
Documente Profesional
Documente Cultură
Example Example
public class myThread extends Thread {
! The previous example wasn’t very interesting public void run() {
because the main thread did nothing for (int i=0; i<5; i++)
System.out.println(“Hello world #“+i);
" Admittedly, this example is not interesting }
}
because the program doesn’t do anything useful,
but we’ll get there eventually public class MyProgram {
public MyProgram() {
! In fact, we could have achieved the same MyThread t = new MyThread();
t.start();
result with no thread at all for (int i=0; i<5; i++)
System.out.println(“foo”);
! So, let’s have the main thread to something }
public static void main(String args[]) {
MyProgram p = new MyProgram();
}
}
RUNNABLE RUNNABLE
start() BLOCKED/ start() BLOCKED/
NEW not WAITING/ NEW not WAITING/
running running TIMED_WAITING running running TIMED_WAITING
TERMINATED TERMINATED
RUNNABLE RUNNABLE
start() BLOCKED/ start() BLOCKED/
NEW not WAITING/ NEW not WAITING/
running running TIMED_WAITING running running TIMED_WAITING
TERMINATED TERMINATED
! 10 threads! ! 14 threads!
" 10 from before, one for each application thread
! In particular, what happens in the RUNNABLE state? pause and give other }
Conclusion