Stopping a Thread

In order to actually stop a thread, the process is quite simple. All you need to do is simply let the run method run out and return. The important thing you need to know is how not to stop a thread, which is using the stop method of the Thread class. The stop method of the Thread class is deprecated along with the method suspend and also the methods resume and countStackFrames that were dependent on suspend in some manner. As we have said, in order to stop a thread, you can simply let the run method finish what it is doing.

public void run() {     // Implement your code…       // Just about to return and the thread will then stop soon after }

Note that the thread will not necessarily be declared finished immediately after the run method has finished, as the JVM still needs to finish it off in the background, but it should terminate completely soon after.

In most cases when you create a thread of your own, you will be running a continuous task of some sort. A good example of creating your own thread is to listen for network messages coming in. Let's suppose that the network messages block, which means that a line of code waiting to return a message will stop where it is until the message has been received. It would not be ideal to call a blocking method such as this in your main loop thread, as the main loop thread needs to execute continuously without stopping to wait for an incoming network message. So the solution is to have your main loop running in one thread and create another thread to listen for incoming network messages on its own. We will look in detail at creating this for multiplayer games toward the end of this book. In order for the network thread to run again and again, it requires a loop. In order to stop a thread that is looping, we can simply declare a Boolean variable to set to false when we want to terminate the thread as follows.

// From outside the run method declare variable   boolean running = true;     public void run() {     while(running)     {        // execute thread code     } }     // Somewhere else, effectively terminate the thread   running = false;

The way in which we have handled stopping threads in this book is by testing to see if the thread reference we used to start the thread is still equal to a reference of the current thread. It's a little complicated to explain but is really quite simple. Let's just jump into the code this time and have a look. The following example, StoppingThread, illustrates the technique we use in this book to handle stopping a thread. Here is the code for StoppingThread.java.

Code Listing 7-3: StoppingThread.java

start example
public class StoppingThread implements Runnable {     public void run()     {        System.out.println("Thread started");          Thread thisThread = Thread.currentThread();        while(loop==thisThread)        {           // Implement looped code, e.g. listen for new network           // messages over and over        }             System.out.println("Thread exited");     }          public void startThread()     {        loop = new Thread(this);        loop.start();     }          public void stopThread()     {        loop = null;     }          public static void main(String args[])     {        // thread loop started in constructor        StoppingThread example = new StoppingThread();        example.startThread();             // now stop the thread like this        example.stopThread();     }       Thread loop; }
end example

When you compile and run this code, the new thread should be started and stopped similarly to the following figure.

click to expand
Figure 7-3:

The Thread while loop is simply controlled by testing if the object referenced by the loop variable is equal to the currently running Thread object referenced by the local variable thisThread. We can retrieve a reference to the running thread using the static method Thread.currentThread(). This method is used to obtain a reference to the current thread that you are running in. You might say it is to threads what the keyword this is to objects. We can then simply terminate the loop, thus leading to the termination of the thread by setting loop equal to null. Now I should point out that our original way of stopping a thread using a Boolean variable will stop the thread also; we just prefer this way.

Okay, brace yourself. We will now take a look at the important issue of thread synchronization and how to handle it. The following sections of this chapter will be kept quite linear to explain the basic concepts of thread synchronization. We will see plenty of practical synchronization examples in the chapters to follow.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net