Java_multithreading
Java_multithreading
Multithreading
Multithreading:
2
Multithreading Basics
Differences between multithreading and multitasking,
thread life cycle,
creating threads,
synchronizing threads,
daemon threads,
thread groups
Multithreading:
3
Unlike many other computer languages, 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 , and each thread
defines a separate path of execution.
Thus, multithreading is a specialized form of multitasking.
You are almost certainly acquainted with multitasking, because it is
supported by virtually all modern operating systems. However, there
are two distinct types of multitasking: process based and thread-
based.
Multithreading:
4
❑PROCESS-BASED MULTITASKING
• A process is a program that is executing. Thus, processbased multitasking is the feature
that allows our computer to run two or more programs concurrently.
• For example, process-based multitasking enables you to run the Java compiler at the same
time that you are using a text editor.
• In process based multitasking, a program is the smallest unit of code that can be
dispatched by the scheduler.
❑THREAD-BASED MULTITASKING
• In a thread-based multitasking environment, the thread is the smallest unit of dispatchable
code. 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.
Multithreading:
5
Differences between Multiprocessing and Multithreading
Multiprocessing Multithreading
More than one process running simultaneousy More than one thread running simultaneously
Its part of a program Its part of a program
It’s a heavy wait process It’s a light wait process
Process is divided into threads Threads are divided into sub-threads
There is no communications between Within the process threads are communicated
processors directly
Multithreading:
6
❑The Java Thread Model
• The Java run-time system depends on threads for many things, and all the class
libraries are designed with multithreading in mind. In fact, Java uses threads to
enable the entire environment to be asynchronous. This helps reduce inefficiency
by preventing the waste of CPU cycles.
• 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.
• Threads are independent so it doesn't affect other threads if exception occur in a single thread.
Multithreading:
8
❑The Java Thread Model ...
Life Cycle of a Thread:
• Instead, a thread’s priority is used to decide when to switch from one running
thread to the next. This is called a context switch.
• The rules that determine when a context switch takes place are simple:
➢A thread can voluntarily relinquish control. This is done by explicitly yielding, sleeping, or blocking on
pending I/O. In this scenario, all other threads are examined, and the highest-priority thread that is ready
to run is given the CPU.
➢A thread can be preempted by a higher-priority thread. In this case, a lower-priority thread that does not
yield the processor is simply preempted - no matter what it is doing - by a higherpriority thread.
Basically, as soon as a higher-priority thread wants to run, it does. This is called preemptive multitasking.
Multithreading:
11
• The Java Thread Model ...
• For example, if you want two threads to communicate and share a complicated data
structure, such as a linked list, you need some way to ensure that they don’t conflict
with each other. That is, you must prevent one thread from writing data while
another thread is in the middle of reading it.
• For this purpose, Java implements an elegant twist on an age-old model of inter-
process synchronization: the monitor.
• The monitor is a control mechanism first defined by C.A.R. Hoare.
Multithreading:
13
• The Java Thread Model ...
Synchronization ...
• A monitor is a very small box that can hold only one thread. Once a thread enters a
monitor, all other threads must wait until that thread exits the monitor. In this way, a
monitor can be used to protect a shared asset from being manipulated by more than
one thread at a time.
• Most multithreaded systems expose monitors as objects that your program must
explicitly acquire and manipulate.
• The synchronization is mainly used to, To prevent thread interference.
To prevent consistency problem.
Multithreading:
14
• The Java Thread Model ...
Synchronization ...
• Java provides a cleaner solution. There is no class “Monitor”; instead,
each object has its own implicit monitor that is automatically entered
when one of the object’s synchronized methods is called.
➢Extending Thread
➢Choosing an Approach
Creating a Thread
• One can create a thread by instantiating an object of type Thread.
• Java defines two ways in which thread can be accomplished: by
implementing the Runnable interface and by extending the Thread
class.
Multithreading:
23
• The only difference is that run( ) establishes the entry point for another, concurrent thread
of execution within your program. This thread will end when run( ) returns.
• After you create a class that implements Runnable, you will instantiate an object of type
Thread from within that class. Thread defines several constructors. The one that we will
use is shown here: Thread(Runnable threadOb, String threadName)
• After the new thread is created, it will not start running until you call its start( ) method,
which is declared within Thread. In essence, start( ) executes a call to run( ).
Deadlock describes a situation where two or more threads are blocked forever, waiting
for each other. Deadlock occurs when multiple threads need the same locks but obtain
them in different order. A Java multithreaded program may suffer from the deadlock
condition because the synchronized keyword causes the executing thread to block while
waiting for the lock, or monitor, associatedwith the specifiedobject.
Multithreading:
Deadlocks for threads ...DeadlockSituation 36
Thread1: Holdinglock1...
Thread2: Holdinglock2...
Thread1: Waitingfor lock 2...
Thread2: Waitingfor lock 1...
So, just changing the order of the locks prevent the program in
going deadlock situation and completes with the following result:
Thread1: Holdinglock1...
Thread1: Waitingfor lock 2...
Thread1: Holdinglock1 & 2...
Thread2: Holdinglock1...
Thread2: Waitingfor lock 2...
Thread2: Holdinglock1 & 2...
Multithreading:
38
❑ Daemon Threads:
In Java, daemon threads are a type of thread that runs in the background to perform tasks that
support the main application.
They are typically used for services such as garbage collection, event handling, or other
background tasks that do not require the application to remain alive.
END