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

Concurrency APIs introduced in Java SE 7

Java SE 7 introduced a major enhancement to its concurrency APIs, mainly focusing on improving support for parallel programming and making better use of multi-core processors. The most significant additions were:
  1. Fork/Join Framework: This framework provides a way to recursively divide a task into smaller subtasks, execute them in parallel, and then combine the results. It is designed to leverage the power of multi-core processors by efficiently managing worker threads and minimizing overhead.
  2. TransferQueue: This is a specialized queue that allows producers to directly transfer elements to consumers, potentially avoiding the need for intermediate buffering and improving performance in certain scenarios.

Besides these major additions, Java SE 7 also included a few other concurrency-related enhancements, such as:
  • Phaser: A more flexible synchronization barrier that allows for multiple phases and dynamic registration of parties.
  • ThreadLocalRandom: A more efficient way to generate random numbers in a multithreaded environment.
  • Enhancements to the existing concurrency utilities: Several minor improvements and additions were made to classes like `Executors`, `ConcurrentHashMap`, and others.

Overall, the concurrency APIs introduced in Java SE 7 were a significant step towards making Java a more capable language for parallel programming and taking advantage of modern hardware.
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

122
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).
In the next lesson, you will explore the basics of threads and multithreaded programming.
[1] multithreading: Multithreading is a CPU feature that allows two or more instruction threads to execute independently while sharing the same process resources.

SEMrush Software