previous
 next 
CS 3733 Operating Systems Notes: Java Threads
(change semester)

  Files are available by executing ~classque/JavaThreads on the CS network.
 
 

There are two main ways to use threads in Java: When you declare a Thread variable with
Thread ping
the variable is initially null.
(This is true for any reference variable.)

If an class implements Runnable it must have a run method. Executing:
ping = new Thread(this)
creates a new thread which can execute the run method of the class. The thread can be started with its start method:
ping.start();
At this point the thread is in its active state. It remains active until it is stopped or it completes execution of its run method.


Ping Pong Application

PingPong.java is a thread that prints out a word a given number of times with a given delay. It also shows the number of active threads.

PingPongTest1.java is an application that just starts two copies of PingPong.

Using join

Join suspends the caller until the thread has completed.

The PingPongTest2 application waits for each thread to complete and then prints a message. It also has a method which shows the threads.


Thread States Note: neither suspend nor stop should be used.


Next Notes

Back to CS 3733 Notes Table of Contents