Java 1.4 Game Programming (Wordware Game and Graphics Library)

Quite similar to the overloaded wait methods that take time parameters, a thread object has two sleep methods: sleep(long timeout) and sleep(long timeout, int nanos). With these methods, you can pause a thread's execution for a specified amount of time. Note that the sleep methods are not concerned with synchronization or monitors or anything like that. We can simply call the sleep method encapsulated in a try/catch block for an InterruptedException (we will look at the intricacies shortly) anywhere in our thread and allow it to pause. The following very simple example shows a simple counter running from 5 to 1, with each countdown taking 1 second (or thereabouts, depending on the resolution of the timer). Here is the code for Countdown.java:

Code Listing 7-4: Countdown.java

public class Countdown { public static void main(String[] args) { System.out.println("and ready in...\n"); for(int i=5; i>0; i--) { System.out.println(i); System.out.print((char)7); // make a bleep sound try { Thread.sleep(1000); // pause for a second } catch(InterruptedException e) { System.out.println(e); } } System.out.println("\nAction!"); } }

That's right—I'm not afraid of the fact that main is itself a thread. As main is running in its own thread like any other, there is no point in us spawning a new thread for this example. When run, the output should be similar to Figure 7-4 and you'll hear some bleep sounds in the countdown.

Figure 7-4:

The code for this example is very straightforward. We call the static method Thread.sleep, which pauses the current thread that invokes this method by the specified amount of time. You might have noticed the bleeping code in this example, where we can simply print ASCII character number 7, which doesn't actually print anything but should make a system bleep every second for the countdown when you run the code.

Note 

If you do not have an internal speaker in your computer, you won't be able to hear anything!

When you send a thread to sleep and "wait" a thread, you are stopping its processor time, allowing other threads to run faster, as they have a higher share of the processor usage. This is important because you might think about making your own special loop to create a time difference, which might work, but as it executes continuously, it takes unnecessary processor time away from other threads that want to run as fast as they can.

There is another important thing to be aware of when calling a thread's sleep method. When calling the sleep method, the thread does not lose ownership of any monitors that it currently owns. When calling the sleep method, the thread does not lose ownership of any monitors that it currently owns. Yes, I said it twice because it's important in the sense that if you are synchronized on an object and then sleep, other threads waiting to execute some fellow synchronized code must wait for the release of that monitor. Hence they will have to wait on a sleep period also. This can be a powerful tool as well as a semantic problem. The important thing to take from this is that you need to analyze exactly what needs to be synchronized and what doesn't and handle your code in that fashion; synchronize to the minimum.

Категории