Distributed Computer Systems
CT024-3-3-DCOMS and Version VC1
Thread Synchronization
Topic & Structure of The
Lesson
• Threads
• Synchronization
• Executors
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 2 of 20
Learning Outcomes
• At the end of this topic, You
should be able to
• Understand the concept of Threads
• Difference between task and thread
• Explaining about the Synchronization
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 3 of 20
Key Terms You Must Be
Able To Use
• If you have mastered this topic, you should be able
to use the following terms correctly in your
assignments and exams:
- Tasks
- Synchronization
- Synchronization model
- Executor
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 4 of 20
Tasks and Threads
• A task is an abstraction of a series of steps
– Might be done in a separate thread
– Java libraries use the Runnable interface
– work done by method run()
• Thread: a Java class for a thread
– work done by method run()
• How to associate a task with a thread?
• How to start a thread?
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 5 of 20
Creating a Task and Thread
• Warning: old way(s), new ways
• First, if you have a thread object, you can call
start() on that object
– Makes it available to be run
– When it’s time to run it, Thread’s run() is called
• So, create a thread using “old” (not good)
way
– Write class that extends Thread, e.g. MyThread
– Define your own run()
– Create a MyThread object and call start() on it
• We won’t do this! Not good design
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 6 of 20
Runnables and Thread
• Use the “task abstraction” and create a
class that implements Runnable interface
– Define the run() method to do the work you
want
• Now, two ways to make your task run in a
separate thread
– Create a Thread object and pass a Runnable
to the constructor
– As before, call start() on the Thread object
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 7 of 20
Do we need a Thread
“manager”?
• If your code is responsible for creating a
bunch of tasks, linking them with Threads,
and starting them all, then you have things
to worry about:
– What if you start too many threads? Can you
manage the number of running threads?
– Can you shutdown all the threads?
– If one fails, can you restart it?
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 8 of 20
Executors
• An Executor is an object that manages
running tasks
– Submit a Runnable to be run with Executor’s
execute() method
– So, instead of creating a Thread for your
Runnable and calling start() on that, do this:
• Get an Executor object, say called exec
• Create a Runnable, say called myTask
• Submit for running: [Link](myTask)
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 9 of 20
How to Get an Executor
• Use static methods in Executors library.
• Fixed “thread pool”: at most N threads running
at one time
Executor exec =
[Link](MAX_THREAD
S);
• Unlimited number of threads
Executor exec =
[Link]();
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 10 of 20
Summary So Far
• Create a class that implements a
Runnable to be your “worker”
• Create Runnable objects
• Create an Executor
• Submit each Runnable to the Executor
which starts it up in a separate thread
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 11 of 20
Synchronization
• Understand the issue with concurrent
access to shared data?
– Data could be a counter (int) or a data
structure (e.g. a Map or List or Set)
• A critical section: a block of code that can
only be safely executed by one thread at a
time
• A lock: an object that is “held” by one
thread at a time, then “released”
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 12 of 20
Synchronization in Java (1)
• Any object can serve as a lock
– Separate object: Object myLock = new
Object();
– Current instance: the this object
• Enclose lines of code in a synchronized block
synchronized(myLock) {
// code here
}
• More than one thread could try to execute this
code, but one acquires the lock and the others
“block” or wait until the first thread releases the
lock
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 13 of 20
Synchronized Methods
• Common situation: all the code in a method is
a critical section
– I.e. only one thread at a time should execute that
method
– E.g. a getter or setter or mutator, or something
that changes shared state info (e.g. a Map of
important data)
• Java makes it easy: add synchronized
keyword to method signature. E.g.
public synchronized void update(…)
{
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 14 of 20
Summary So Far
• Concurrent access to shared data
– Can lead to serious, hard-to-find problems
– E.g. race conditions
• The concept of a lock
• Synchronized blocks of code or methods
– One thread at a time
– While first thread is executing it, others block
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 15 of 20
More Advanced
Synchronization
• A semaphore object
– Allows simultaneous access by N threads
– If N==1, then this is known as a mutex (mutual
exclusion)
– Java has a class Semaphore
• Java class CountDownLatch
– Created with a count (often a number of “worker”
threads). Say object is allWorkersDone
– Another thread (a “manager”) waits for all the workers
to call countDown() on that object
– So manager blocks with:
[Link]()
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 16 of 20
Deadlock
Definition : A set of processes is deadlocked if each process in the set is waiting for an
event that only another process in the set can cause
Necessary conditions for deadlock :
• Mutual Exclusion : each resource is either currently assigned to one process or is
available to be assigned
• Hold and wait : processes currently holding resources granted earlier can request
new resources
• Non-Pre-emption : resources previously granted cannot arbitrarily be taken away
from a process; they must be explicitly released by the process
• Circular wait : there must be a circular chain of two or more processes, each of
which is waiting for a resource held by the next member of the chain
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 17 of 20
‹#›
Summary of last section
• Multiple threads may need to cooperate
– Common situation: some workers and a manager
– One thread may need to wait for one or more
thread to complete
– One or more threads may need to wait to be
“released”
– Or a combination of these situations
• Threads all access a CountDownLatch
– await() used to wait for enough calls to
countDown()
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 18 of 20
Question and answer session
Q&A
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 19 of 20
What we will cover next
• Socket Programming
CT024-3-3 DCOMS-Distributed Computer Systems Thread Synchronization 20 of 20