32 Slide
32 Slide
Parallel Programming
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
1
Objectives
To get an overview of multithreading (§32.2).
To develop task classes by implementing the Runnable interface (§32.3).
To create threads to run tasks using the Thread class (§32.3).
To control threads using the methods in the Thread class (§32.4).
To control animations using threads and use Platform.runLater to run the code in application
thread (§32.5).
To execute tasks in a thread pool (§32.6).
To use synchronized methods or blocks to synchronize threads to avoid race conditions (§32.7).
To synchronize threads using locks (§32.8).
To facilitate thread communications using conditions on locks (§§32.9–32.10).
To use blocking queues to synchronize access to an array queue, linked queue, and priority
queue (§32.11).
To restrict the number of accesses to a shared resource using semaphores (§32.12).
To use the resource-ordering technique to avoid deadlocks (§32.13).
To describe the life cycle of a thread (§32.14).
To create synchronized collections using the static methods in the Collections class (§32.15).
To develop parallel programs using the Fork/Join Framework (§32.16).
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
2
Threads Concept
Multiple
threads on
multiple
CPUs
Multiple
threads
sharing a
single CPU
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
3
Creating Tasks and Threads
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
4
Example:
Using the Runnable Interface to
Create and Launch Threads
Objective: Create and run three threads:
– The first thread prints the letter a 100 times.
– The second thread prints the letter b 100 times.
– The third thread prints the integers 1 through
100.
TaskThreadDemo
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
5
The Thread Class
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
6
The Static yield() Method
You can use the yield() method to temporarily release time
for other threads. For example, suppose you modify the
code in Lines 53-57 in TaskThreadDemo.java as follows:
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
7
The Static sleep(milliseconds) Method
The sleep(long mills) method puts the thread to sleep for the specified
time in milliseconds. For example, suppose you modify the code in
Lines 53-57 in TaskThreadDemo.java as follows:
Every time a number (>= 50) is printed, the print100 thread is put to
sleep for 1 millisecond.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
8
The join() Method
You can use the join() method to force one thread to wait for another
thread to finish. For example, suppose you modify the code in Lines
53-57 in TaskThreadDemo.java as follows:
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
11
Thread Priority
Each thread is assigned a default priority of
Thread.NORM_PRIORITY. You can reset the
priority using setPriority(int priority).
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
12
Example: Flashing Text
FlashText
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
13
Thread Pools
Starting a new thread for each task could limit throughput and cause
poor performance. A thread pool is ideal to manage the number of
tasks executing concurrently. JDK 1.5 uses the Executor interface for
executing tasks in a thread pool and the ExecutorService interface for
managing and controlling tasks. ExecutorService is a subinterface of
Executor.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
14
Creating Executors
To create an Executor object, use the static methods in the Executors
class.
ExecutorDemo
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
15
Thread Synchronization
1 0 newBalance = bank.getBalance() + 1;
2 0 newBalance = bank.getBalance() + 1;
3 1 bank.setBalance(newBalance);
4 1 bank.setBalance(newBalance);
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
16
Example: Showing Resource Conflict
Objective: Write a program that demonstrates the problem of
resource conflict. Suppose that you create and launch one hundred
threads, each of which adds a penny to an account. Assume that the
account is initially empty.
AccountWithoutSync
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
17
Race Condition
What, then, caused the error in the example? Here is a possible scenario:
1 0 newBalance = balance + 1;
2 0 newBalance = balance + 1;
3 1 balance = newBalance;
4 1 balance = newBalance;
);
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
19
Synchronizing Instance Methods and
Static Methods
A synchronized method acquires a lock before it executes.
In the case of an instance method, the lock is on the object
for which the method was invoked. In the case of a static
method, the lock is on the class. If one thread invokes a
synchronized instance method (respectively, static method)
on an object, the lock of that object (respectively, class) is
acquired first, then the method is executed, and finally the
lock is released. Another thread invoking the same method
of that object (respectively, class) is blocked until the lock
is released.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
20
Synchronizing Instance Methods and
Static Methods
With the deposit method synchronized, the preceding scenario cannot
happen. If Task 2 starts to enter the method, and Task 1 is already in
the method, Task 2 is blocked until Task 1 finishes the method.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
21
Synchronizing Tasks
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
22
Synchronizing Statements
Invoking a synchronized instance method of an object acquires a lock
on the object, and invoking a synchronized static method of a class
acquires a lock on the class. A synchronized statement can be used to
acquire a lock on any object, not just this object, when executing a
block of the code in a method. This block is referred to as a
synchronized block. The general form of a synchronized statement is
as follows:
synchronized (expr) {
statements;
}
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
24
Synchronization Using Locks
A synchronized instance method implicitly acquires a lock on the instance before it
executes the method.
JDK 1.5 enables you to use locks explicitly. The new locking features are flexible
and give you more control for coordinating threads. A lock is an instance of the
Lock interface, which declares the methods for acquiring and releasing locks. A
lock may also use the newCondition() method to create any number of Condition
objects, which can be used for thread communications.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
25
Fairness Policy
ReentrantLock is a concrete implementation of Lock for
creating mutual exclusive locks. You can create a lock with
the specified fairness policy. True fairness policies
guarantee the longest-wait thread to obtain the lock first.
False fairness policies grant a lock to a waiting thread
without any access order. Programs using fair locks
accessed by many threads may have poor overall
performance than those using the default setting, but have
smaller variances in times to obtain locks and guarantee
lack of starvation.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
26
Example: Using Locks
This example revises AccountWithoutSync.java to
synchronize the account modification using explicit locks.
AccountWithSyncUsingLock
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
27
Cooperation Among Threads
The conditions can be used to facilitate communications among threads.
A thread can specify what to do under a certain condition. Conditions
are objects created by invoking the newCondition() method on a Lock
object. Once a condition is created, you can use its await(), signal(), and
signalAll() methods for thread communications. The await() method
causes the current thread to wait until the condition is signaled. The
signal() method wakes up one waiting thread, and the signalAll()
method wakes all waiting threads.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
28
Cooperation Among Threads
To synchronize the operations, use a lock with a condition:
newDeposit (i.e., new deposit added to the account). If the
balance is less than the amount to be withdrawn, the
withdraw task will wait for the newDeposit condition. When
the deposit task adds money to the account, the task signals
the waiting withdraw task to try again.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
29
Example: Thread Cooperation
Write a program that demonstrates thread cooperation. Suppose that
you create and launch two threads, one deposits to an account, and
the other withdraws from the same account. The second thread has to
wait if the amount to be withdrawn is more than the current balance
in the account. Whenever new fund is deposited to the account, the
first thread notifies the second thread to resume. If the amount is still
not enough for a withdrawal, the second thread has to continue to
wait for more fund in the account. Assume the initial balance is 0 and
the amount to deposit and to withdraw is randomly generated.
ThreadCooperation
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
30
Java’s Built-in Monitors (Optional)
Locks and conditions are new in Java 5. Prior to Java 5,
thread communications are programmed using object’s built-
in monitors. Locks and conditions are more powerful and
flexible than the built-in monitor. For this reason, this section
can be completely ignored. However, if you work with legacy
Java code, you may encounter the Java’s built-in monitor. A
monitor is an object with mutual exclusion and
synchronization capabilities. Only one thread can execute a
method at a time in the monitor. A thread enters the monitor
by acquiring a lock on the monitor and exits by releasing the
lock. Any object can be a monitor. An object becomes a
monitor once a thread locks it. Locking is implemented using
the synchronized keyword on a method or a block. A thread
must acquire a lock before executing a synchronized method
or block. A thread can wait in a monitor if the condition is not
right for it to continue executing in the monitor.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
31
wait(), notify(), and notifyAll()
Use the wait(), notify(), and notifyAll() methods to facilitate
communication among threads.
The wait() method lets the thread wait until some condition occurs.
When it occurs, you can use the notify() or notifyAll() methods to
notify the waiting threads to resume normal execution. The
notifyAll() method wakes up all waiting threads, while notify() picks
up only one thread from a waiting queue.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
32
Example: Using Monitor
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
34
Case Study: Producer/Consumer (Optional)
Listing 32.8 presents the complete program. The program contains
the Buffer class (lines 43-89) and two tasks for repeatedly producing
and consuming numbers to and from the buffer (lines 15-41). The
write(int) method (line 58) adds an integer to the buffer. The read()
method (line 75) deletes and returns an integer from the buffer.
ConsumerProducer
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
35
Blocking Queues (Optional)
§22.8 introduced queues and priority queues. A blocking
queue causes a thread to block when you try to add an
element to a full queue or to remove an element from an
empty queue.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
36
Concrete Blocking Queues
Three concrete blocking queues ArrayBlockingQueue,
LinkedBlockingQueue, and PriorityBlockingQueue are supported in JDK
1.5. All are in the java.util.concurrent package. ArrayBlockingQueue
implements a blocking queue using an array. You have to specify a
capacity or an optional fairness to construct an ArrayBlockingQueue.
LinkedBlockingQueue implements a blocking queue using a linked list.
You may create an unbounded or bounded LinkedBlockingQueue.
PriorityBlockingQueue is a priority queue. You may create an unbounded
or bounded priority queue.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
37
Producer/Consumer Using Blocking Queues
The program gives an example of using an ArrayBlockingQueue for
the Consumer/Producer problem.
ConsumerProducerUsingBlockingQueue
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
38
Semaphores (Optional)
Semaphores can be used to restrict the number of threads
that access a shared resource. Before accessing the resource,
a thread must acquire a permit from the semaphore. After
finishing with the resource, the thread must return the permit
back to the semaphore.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
39
Creating Semaphores
To create a semaphore, you have to specify the number of permits
with an optional fairness policy. A task acquires a permit by
invoking the semaphore’s acquire() method and releases the permit
by invoking the semaphore’s release() method. Once a permit is
acquired, the total number of available permits in a semaphore is
reduced by 1. Once a permit is released, the total number of
available permits in a semaphore is increased by 1.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
40
Deadlock
Sometimes two or more threads need to acquire the locks on several shared
objects. This could cause deadlock, in which each thread has the lock on one of the
objects and is waiting for the lock on the other object. Consider the scenario with
two threads and two objects. Thread 1 acquired a lock on object1 and Thread 2
acquired a lock on object2. Now Thread 1 is waiting for the lock on object2 and
Thread 2 for the lock on object1. The two threads wait for each other to release the
in order to get the lock, and neither can continue to run.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
41
Preventing Deadlock
Deadlock can be easily avoided by using a simple technique known
as resource ordering. With this technique, you assign an order on all
the objects whose locks must be acquired and ensure that each
thread acquires the locks in that order. For the example, suppose the
objects are ordered as object1 and object2. Using the resource
ordering technique, Thread 2 must acquire a lock on object1 first,
then on object2. Once Thread 1 acquired a lock on object1, Thread 2
has to wait for a lock on object1. So Thread 1 will be able to acquire
a lock on object2 and no deadlock would occur.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
42
Thread States
A thread can be in one of five
states: New, Ready, Running,
Blocked, or Finished.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
43
Synchronized Collections
The classes in the Java Collections Framework are not thread-safe,
i.e., the contents may be corrupted if they are accessed and updated
concurrently by multiple threads. You can protect the data in a
collection by locking the collection or using synchronized collections.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
44
The Fork/Join Framework
The widespread use of multicore systems
has created a revolution in software. In
order to benefit from multiple processors,
software needs to run in parallel.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
45
The Fork/Join Framework
The Fork/Join Framework is used for parallel
programming in Java.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
46
ForkJoinTask and ForkJoinPool
The framework defines a task using the
ForkJoinTask class, and executes a task in an
instance of ForkJoinPool.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
47
ForkJoinTask
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
48
Examples
ParallelMergeSort ParallelMax
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
49
Companion
Website Vector, Stack, and Hashtable
Invoking synchronizedCollection(Collection c) returns a new Collection
object, in which all the methods that access and update the original
collection c are synchronized. These methods are implemented using the
synchronized keyword. For example, the add method is implemented
like this:
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020
Pearson Education, Inc. All rights reserved.
51