MultiThreaded Programming  «Prev  Next»

Lesson 1

Multithreaded Java Programming

The Java Platform supports multithreaded or concurrent programming with the
  1. Thread Class and
  2. Runnable interface
since Java 1.0. Java 5.0 increased that support with a comprehensive set of new utilities for concurrent programming.
Multithreading is a powerful function that allows a program to perform multiple tasks at once. Many modern applications rely on multithreading to perform background operations while the user is working on something in the foreground. Multithreading[1] also plays an important role in processing input from graphical user interfaces.
This module introduces you to multithreading as it relates to Java. You will learn how to create and use threads, and then you will create an applet that uses threads as a timing mechanism.

Learning Objectives

After completing the module, you will have the skills and knowledge necessary to:
  1. Understand the significance of threads
  2. Create and use threads
  3. Put threads to sleep
  4. Understand the importance of thread synchronization
In the next lesson, you will explore the basics of threads and multithreaded programming.
The following quiz question deals with the wait() method within the context of Java Threads.


Effect of calling the wait() method on an Object

Which of the following correctly describes the effect of calling the wait() method on an object?
(Assume that the calling Thread has the lock of that object.)
[Select 1 option: ]
  1. The thread that has called the wait() method will not come out of the WAITING state until some other thread calls notify() or notifyAll() on the same object or interrupts the thread.
  2. The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
  3. An exception might be raised if it is not called in a synchronized block otherwise no exception will be raised.
  4. The thread calling wait() will be automatically synchronized with any other threads who call wait().
  5. None of the above


Answer: a
Explanation:
b. Note that it is not the object that calls the wait/notify but the thread.
c. It will ALWAYS raise IllegalMonitorStateException if not called in a synchronized block for that object.
d. Only if all the threads call wait on the same object.
A Thread should have the monitor of that object before calling wait() on it. So if a thread synchronizes on some object and calls wait() on another object, then it will throw an IllegalMonitorStateException. Once a thread calls wait() on an object, it will wait until this thread is asked to come out of the wait state. This happens when any of the following happens.
  1. the time in milliseconds passed in as a parameter to wait() elapses.
  2. some other thread calls notifyAll on the same object.
  3. some other thread calls notify on the same object AND the JVM picks this particular thread (out of all the threads that might have called wait() on the same object) to wake up.
  4. some other thread calls this thread's interrupt method.
  5. In all the above cases, the waiting thread comes out of the WAITING state but goes into BLOCKED state. It must still reacquire the lock before continuing further (because it is still within the synchronized block).
[1] multithreading: Multithreading is a CPU feature that allows two or more instruction threads to execute independently while sharing the same process resources.