MultiThreaded Programming  «Prev  Next»
Lesson 1

Introduction to Multi Threaded Java Programming

The Java Platform has supported multi-threaded or concurrent programming with the
  1. Thread Class
  2. Runnable interface
since Java 1.0. Java 5.0 increases 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 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
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).