0% found this document useful (0 votes)
23 views30 pages

Chapter 5 Multi-Threading

Uploaded by

Salih Akadar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views30 pages

Chapter 5 Multi-Threading

Uploaded by

Salih Akadar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Advanced Programming

with Java

Presented by,
Abdalla M.,
Department of Computer Science,
Collage of Engineering and Technology,
Samara University.

1
Chapter 5
Multi threading

2
Multithreading in Java

Multithreading in java is a process of


executing multiple threads
simultaneously.
Thread is basically a lightweight sub-
process, a smallest unit of processing.
Multiprocessing and multithreading, both
are used to achieve multitasking.

3
Multithreading in Java

But we use multithreading than


multiprocessing because threads share a
common memory area.
They don't allocate separate memory area
so saves memory, and context-switching
between the threads takes less time than
process.
Java Multithreading is mostly used in
games, animation etc.
4
Advantage of Java
Multithreading
1) It doesn't block the user because
threads are independent and you can
perform multiple operations at same time.
2) You can perform many operations
together so it saves time.
3) Threads are independent so it doesn't
affect other threads if exception occurs in a
single thread.

5
Multitasking

Multitasking is a process of executing


multiple tasks simultaneously.
We use multitasking to utilize the CPU.
Multitasking can be achieved by two
ways:
◦ Process-based Multitasking(Multiprocessing)
◦ Thread-based Multitasking(Multithreading)

6
Multitasking
1) Process-based Multitasking
(Multiprocessing)
◦ Each process has its own address in memory i.e.
each process allocates separate memory area.
◦ Process is heavyweight.
◦ Cost of communication between the processes is
high.
◦ Switching from one process to another require
some time for saving and loading registers,
memory maps, updating lists etc.

7
Multitasking
2) Thread-based Multitasking
(Multithreading)
◦ Threads share the same address space.
◦ Thread is lightweight.
◦ Cost of communication between the thread is
low.
Note: At least one process is required for
each thread.

8
What is Thread in java

A thread is a lightweight sub process, a


smallest unit of processing.
It is a separate path of execution.
Threads are independent, if there occurs
exception in one thread, it doesn't affect
other threads.
It shares a common memory area.

9
What is Thread in java

10
Life cycle of a Thread (Thread States)

A thread can be in one of the five states.


The life cycle of the thread in java is
controlled by JVM.
The java thread states are as follows:
◦ New
◦ Runnable
◦ Running
◦ Non-Runnable (Blocked)
◦ Terminated
11
Life cycle of a Thread
(Thread States)

12
Life cycle of a Thread
(Thread States)

1) New
◦ The thread is in new state if you create an
instance of Thread class but before the
invocation of start() method.
2) Runnable
◦ The thread is in runnable state after invocation
of start() method, but the thread scheduler has
not selected it to be the running thread.

13
Life cycle of a Thread
(Thread States)

3) Running
◦ The thread is in running state if the thread
scheduler has selected it.
4) Non-Runnable (Blocked)
◦ This is the state when the thread is still alive,
but is currently not eligible to run.
5) Terminated
◦ A thread is in terminated or dead state when
its run() method exits.

14
How to create thread

There are two ways to create a thread:


◦ By extending Thread class
◦ By implementing Runnable interface.

15
Thread class:

Thread class provide constructors and


methods to create and perform operations
on a thread.
Thread class extends Object class and
implements Runnable interface.

16
Runnable interface:

The Runnable interface should be


implemented by any class whose
instances are intended to be executed by a
thread.
Runnable interface have only one method
named run().
public void run(): is used to perform
action for a thread.

17
Starting a thread:

start() method of Thread class is used to


start a newly created thread.
It performs following tasks:
A new thread starts(with new callstack).
The thread moves from New state to the
Runnable state.
When the thread gets a chance to execute,
its target run() method will run.

18
By extending Thread class:

class Multi extends Thread{


public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
} }
Output: thread is running...
19
By implementing the Runnable
interface:
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
} }
Output: thread is running...
20
Thread Scheduler in Java

Thread scheduler in java is the part of


the JVM that decides which thread should
run.
There is no guarantee that which runnable
thread will be chosen to run by the thread
scheduler.
Only one thread at a time can run in a
single process.

21
Difference between pre-emptive scheduling and time
slicing

The thread scheduler mainly uses pre-


emptive or time slicing scheduling to
schedule the threads.
Under pre-emptive scheduling, the
highest priority task executes until it
enters the waiting or dead states or a
higher priority task comes into existence.

22
Difference between pre-emptive scheduling and time
slicing

Under time slicing, a task executes for a


predefined slice of time and then re-enters
the pool of ready tasks.
The scheduler then determines which task
should execute next, based on priority and
other factors.

23
Sleep method in java
The sleep() method of Thread class is
used to sleep a thread for the specified
amount of time.

24
Can we start a thread twice?

No. After starting a thread, it can never


be started again.
If you does so,
an IllegalThreadStateException is
thrown.
In such case, thread will run once but for
second time, it will throw exception.

25
Thread Priority:

Each thread have a priority.


Priorities are represented by a number
between 1 and 10.
In most cases, thread schedular schedules
the threads according to their priority
(known as pre-emptive scheduling).
 But it is not guaranteed because it
depends on JVM specification that which
scheduling it chooses.
26
Thread Priority:

3 constants defined in Thread class:


1) public static int MIN_PRIORITY
2) public static int NORM_PRIORITY
3) public static int MAX_PRIORITY

27
Thread Priority:

Default priority of a thread is 5


(NORM_PRIORITY).
The value of MIN_PRIORITY is 1 and
the value of MAX_PRIORITY is 10.

28
Examples
How to perform single task by multiple
threads?
How to perform multiple tasks by
multiple threads (multitasking in
multithreading)?

29
The End!!!
Thank You?
Questions?

30

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy