0% found this document useful (0 votes)
5 views10 pages

Java Multithreading Overview

The document discusses multithreading in Java, explaining how it allows a program to be divided into sub-programs that execute concurrently. It covers the creation of threads, their lifecycle, and the importance of synchronization and inter-thread communication. Additionally, it highlights the differences between multithreading and multitasking, emphasizing the efficiency of using threads for concurrent execution.

Uploaded by

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

Java Multithreading Overview

The document discusses multithreading in Java, explaining how it allows a program to be divided into sub-programs that execute concurrently. It covers the creation of threads, their lifecycle, and the importance of synchronization and inter-thread communication. Additionally, it highlights the differences between multithreading and multitasking, emphasizing the efficiency of using threads for concurrent execution.

Uploaded by

sumsi.minhaj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Multithreading

Multithreaded Programming
MULTITHREADING

Multitasking
-
: OS -
System Level Task

multiple take/pgme executed by OS

supports multithreading
M only java
Multithreading : In Java a
,
pgm can be divided into sub-pyms'; each
sub-pgm will be executed
parallelly
main()
A thread is
pym
with
single flow of control
-

a a

-
-

- -

-
--

control
Multithreading
has numerous or more than one
flow of
main()
* Java
multithreading to implement concurrency
-

uses
---

Java threade
lightweight threads/processes
* -

are called . This is


- -

---

sub threads the their


memory
cos use same
space as

build- picture() display-picture


main method
.
--- -
-

> S
-
--
- -

--- -
--
MULTITHREADING MULTITASKING

Programming concept OS concept


-
-

Multiple sub-pgms -Multiple programs/tacks


-

are

executed in parallel are executed in parallel


is the unallest unit
Thread is the smallest
unit-Program
-

Processor switches between -

Process orwwitches between diff

diff threads

I
processes

More
efficient Less
efficient
-
-
Creating Threads
I
ways for creating a new thread

thread that extends (single


By creating class-defines class thread class inheritance)
-

a a .

sparent class
class ThreadA extends Thread

By converting a clase to a
thread-defines a class that implements a Runnable
Interface multiple Inheritance)
class Thread A implements Interface

Thread
Creating by Extending Thread Classes

Multithreading/Tasking -run() predefined thread class


-

Thread = -makes the entire of the thread


body
Steps involved only method on which the thread behaviour can be implemented

1 . Declare a class should be invoked by an


obj ofthread cas

.
c
Implement run() method public void run 2)

E
.
3 Create thread obj I call start() method -
-

class Athread extends Thread 3


↓ ↓ ↓
new clase kn predefined
created class

class MyThread extends Thread Gi-class A extende Thread


E E

public void run C public void run()

E E
-

- for (inti = 1
; i < 5 ; i + +)

3-
E
"

printin ("From thread i);


System. out A : i = +

>
-

Creating an object se stem out .


.

printen ("Exit from A") ;


3
Clarename objname = new dasname ; 3
class
obj
MyThread A =
new
MyThread 2) ; class B extends Thread
E
A. start() public void run C
OR E
new
MyThread() start()a"
.
for (intj > 1
; j <= 5 ; j + + )
E
("From thread B: j
"
+ j) ;
System. out printin
=
.

system. out printen ("Exit from B" From thread A : i =


2
3
3 B
From thread :
j =

class ThreadTest
C static void main (String args [])
public
&
new All Start 2);
.

2);
new B2) Start .

3
3
Life Thread Runnable Blocked
Cycle of 2 .
State 4 . State

State Not thread


Newborn
allowing enter
1 static void main CS to
.

public a

E
.
2 Runnable State A obj = new ACC; the runnable state
/running
.
3
Running State dan objA .
Start(); >
-
thread becomes runnable suspend () , sleep ()
3 Tready to beCPU
executed by
4 .
Blocked State

.
5 Dead State 3 .

Running State .
5 Dead State

The executed Whenever thread has executed


thread is
being by completely
-

processor

Newborn time time slots to threads


1 State
slicing signing its run()
>
-
.
:- a

When create thread >


yield () relinquish control to another thread is not able to out
of the
blockedo
-

we a
-
: -
come

Notscheduled
for running O To

5 Sec
↓ 5sec
-

Either enter runnable or dead state 5 Sea

executed by
processor
O
Runnable
class A extends Thread
E -
Suspend() > for -
a particular time
void ( period thread is its ctrl
run
passing
sleep (time) >
-

F
- - >

O enable
resume

Running
2)
pended
Thread
Priority
-

T Te ,
Ts
,

-3
priority being given
MIN - PRIORITY

MAX -
PRIORITY

NORM-PRIORITY

&

setPriority (

Thread Name setPriority (int-number) ;


int-number is the int value used to set the
priority
>
-

>
-

value from 1-10

High priority threads can


preumpt bu
priority threads
Creating Thread by Implementing Runnable Interfaces

Steps

Declare a class that implements runnable interface


Define the run C)

Create thread that instantiated by the runnable


a
by defining an
obj is
interface
class X implements
E
public void runc)

=
class Runnable Thread
S
public static void main (String arge(2)
E
X runnable = new X() ;

Thread Thread : new Thread Crunnable)


threadX Start c)
.
;
3
3
Synchronisation
Threads need from
to make use
of data methods another clas

don't write
* deadlock will occur
if
we
synchronized
synchronized void update
E
3

dans ThreadA extends Thread


E
public void run (

= data member
I methods
3

3
Inter-Thread Communication

Exchange of between
diff threads
-

msg
It takes
place before after the
change of state
-

or

Active thread will notify the suspended threads before it suspended


enters the state
-

3 methods
notify 2)
-
-

()
-notifyall
wait()
-

You might also like