The JavaTM Tutorial
Previous Page Lesson Contents Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Essential Java Classes
Lesson: Threads: Doing Two or More Tasks At Once

Answers to Questions and Exercises: Threads

Questions

  1. Question: What method in Timer or TimerTask can you use to determine when the task was most recently scheduled to execute? (Hint: You can find the answer by looking at the API documentation for Timer and TimerTask. Remember that these classes were introduced in version 1.3 of the Java platform.)

    Answer: The TimerTask scheduledExecutionTime method.

  2. Question: What is the effect of calling the start method on a Thread object?

    Answer: The thread goes into the Runnable state and the run method is invoked.

  3. Question: What are the two ways you can provide the implementation for a thread's run method?

    Answer:

    • Create a Thread subclass that overrides the run method.
    • Write a class that implements the Runnable interface and the run method.

Exercises

  1. Exercise: Convert AnnoyingBeep.java(in a .java source file) so that the initial delay is 5 seconds, instead of 0.

    Solution: Change the initial delay argument to the schedule method from 0 to 5*1000.

  2. Exercise: Convert AnnoyingBeep.java(in a .java source file) to use the scheduleAtFixedRate method instead of schedule to schedule the task. Change the implementation of the run method so that if the run method is called too late for a warning beep (say, more than 5 milliseconds after it was scheduled to run), nothing happens--no beep and string are generated. (Hint: Remember your answer to question 1.)

    Solution: Substitute scheduleAtFixedRate for schedule. You can use System.currentTimeMillis() to get the current time to compare with the scheduled execution time. A full solution is in SkippingBeep.java(in a .java source file).

  3. Exercise: Change the main program of TwoThreadsDemo.java(in a .java source file) so that it creates a third thread, named "Bora Bora." ” Compile and run the program again. Note that you will also need SimpleThread.java(in a .java source file). Does this change your vacation destiny?

    Solution: Add the following line to the main method:

    new SimpleThread("Bora Bora").start();
    
  4. Exercise: Compile and run RaceDemo.java(in a .java source file) and SelfishRunner.java(in a .java source file) on your computer. Do you have a time-sliced system?

    Solution: The answer depends on your system. As of JDK 1.0, the Java VM on Win32 (includes Windows 95/98/NT/2000) uses time slicing while the Java VM on Solaris does not.

  5. Exercise: Well-behaved threads voluntarily relinquish the CPU periodically and give other threads an opportunity to run. Rewrite the SelfishRunner(in a .java source file) class to be a PoliteRunner. Be sure to modify the main program in RaceDemo.java(in a .java source file) to create PoliteRunners instead of SelfishRunners.

    Solution: Add a call to the yield method somewhere within the run method, such as just after the call to println.


Previous Page Lesson Contents Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.