Lesson 1
Multithreaded Java Programming
The Java Platform supports multithreaded or concurrent programming with the
- Thread Class and
- 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:
- Understand the significance of threads
- Create and use threads
- Put threads to sleep
- 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:
- 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.
- 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: ]
- 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.
- The object issuing the call to
wait()
will halt until another object sends a notify()
or notifyAll()
method
- An exception might be raised if it is not called in a synchronized block otherwise no exception will be raised.
- The thread calling wait() will be automatically synchronized with any other threads who call wait().
- 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.
- the time in milliseconds passed in as a parameter to wait() elapses.
- some other thread calls notifyAll on the same object.
- 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.
- some other thread calls this thread's interrupt method.
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.