Chapter 5
Chapter 5
• We can walk, talk, breathe, see, hear, smell... all at the same time
• Computers can do this as well - download a file, print a file, receive email, run the
clock
• Only computers that have multiple processors can truly execute multiple instructions
concurrently
• In this single tasking the microprocessor will be sitting idle for most of the time. This
means micro processor time is wasted.
• For example, process based multitasking enables you to run and use Microsoft
words at the same time you can use a text editor or visiting a web site.
• This means that a single program can perform two or more tasks
simultaneously.
• For instance, a text editor can format text at the same time that it is printing,
as long as these two actions are being performed by two separate threads.
• Thus, process-based multitasking deals with the “big picture” and thread-
based multitasking handles the details.
Single thread and multithread
• In this model, a single thread of control runs in an infinite loop, polling a single
event queue to decide what to do next.
• For instance, a network file is ready to be read, then the event loop dispatches
control to the appropriate event handler.
• Until this event handler returns, nothing else can happen in the program.
• It can also result in one part of a program dominating the system and preventing any
other events from being processed.
Single threaded and multithreaded
• In general, in a single-threaded environment, when a thread blocks (that is, suspends
execution) because it is waiting for some resource, the entire program stops running.
• For example, the idle time created when a thread reads data from a network or waits
for user input can be utilized elsewhere.
• Multithreading allows animation loops to sleep for a second between each frame
without causing the whole system to pause.
• When a thread blocks in a Java program, only the single thread that is blocked pauses.
All other threads continue to run.
Multithreading in java
• Java provides built-in support for multithreaded programming.
• A multithreaded program contains two or more parts that can run concurrently. Each
part of such a program is called a thread.
Uses of Threads:
• Threads are used in designing server side programs to handle multiple clients at a
time.
• Write public void run () method in that class. This is the method by
default executed by any thread
8
Thread Synchronization
9
Thread Synchronization
1) Mutual Exclusive
1. Synchronized method.
2. Synchronized block.
10
Mutual Exclusive
Mutual Exclusive helps keep threads from interfering with one another while
sharing data. This can be done by two ways in java:
1) by synchronized method
2) by synchronized block
Concept of Lock in Java
• Synchronization is built around an internal entity known as the lock or monitor. Every
object has a lock associated with it. By convention, a thread that needs consistent
access to an object's fields has to acquire the object's lock before accessing them, and
then release the lock when it's done with them.
11
Mutual Exclusive
i) Synchronized method
• Syntax:
12
Mutual Exclusive
• If you put all the codes of the method in the synchronized block, it will work same
as the synchronized method.
13
Inter-thread communication
1) wait()
2) notify()
3) notifyAll()
14
Inter-thread communication
1) wait() method
Causes current thread to release the lock and wait until either another thread invokes
the notify() method or the notifyAll() method for this object, or a specified amount of
time has elapsed.
The current thread must own this object's monitor, so it must be called from the
synchronized method only otherwise it will throw exception.
15
Inter-thread communication
2) notify() method
16
Inter-thread communication
17
Deadlock
18