0% found this document useful (0 votes)
8 views34 pages

4.1 Multithreading

The document provides an overview of multithreading in Java, explaining the concept of threads, their execution in single and multithreaded processes, and the benefits of using multithreading for better response times and higher throughput. It details various threading mechanisms, including extending the Thread class and implementing the Runnable interface, as well as the importance of synchronization and inter-thread communication to avoid race conditions. Additionally, it covers practical examples of thread creation, manipulation, and handling synchronization issues, including deadlock scenarios.

Uploaded by

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

4.1 Multithreading

The document provides an overview of multithreading in Java, explaining the concept of threads, their execution in single and multithreaded processes, and the benefits of using multithreading for better response times and higher throughput. It details various threading mechanisms, including extending the Thread class and implementing the Runnable interface, as well as the importance of synchronization and inter-thread communication to avoid race conditions. Additionally, it covers practical examples of thread creation, manipulation, and handling synchronization issues, including deadlock scenarios.

Uploaded by

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

Multithreading in Java

(A built-in feature in Java)

1
What are Threads?

 Thread is a piece of code that can execute


in concurrence with other threads.
 It is a schedule entity on a processor

Hardware
Context Local state
Global/ shared state
PC
Registers
Registers

Status
StatusWord
Word
Hard/Software Context
Program
ProgramCounter
Counter

Running Thread Object


2
Single and Multithreaded
Processes

Single-threaded Process Multiplethreaded


Threads of Process
Execution

Single instruction stream Common Multiple instruction stream


Address Space

3
OS:
Multi-Processing, Multi-
Threaded
Threaded Libraries, Multi-threaded I/O

Application

Application Application

Application
CPU
CPU
CPU CPU CPU CPU

Better Response Times in Higher Throughput for


Multiple Application Parallelizeable Applications
Environments
4
Threaded Process Model

THREAD
THREAD
STACK
STACK

SHARED
SHARED
MEMOR
MEMOR
YY
THREAD
THREAD
DATA
DATA
Threads within a process THREAD
THREAD
TEXT
TEXT
 Independent executables
 All threads are parts of a process hence communication
easier and simpler. 5
Levels
Levels ofof
Parallelism:
Parallelism:
Thread
Thread Granularity
Granularity
Code- Code-
Granularity
Granularity
Task
Taski-l
i-l Task
Taskii Task
Taski+1
i+1 Code
CodeItem
Item
Large
Largegrain
grain
(task
(tasklevel)
level)
Program
Program
func1
func1( () ) func2
func2( () ) func3
func3( () )

 Task
Task
{{
....
{{
....
{{
.... Medium
 .... .... .... Mediumgrain
grain
 Control
Control
....
}}
.... ....
}}
.... ....
}}
.... (control
(controllevel)
level)

 Data
Data Function
Function(thread)
(thread)

 Multiple
MultipleIssue
Issue
aa( (00) )=.. aa( (11)=.. aa( (22)=.. Fine
=..
bb( (00) )=..
)=..
bb( (11)=..
)=..
bb( (22)=.. Finegrain
grain
=.. )=.. )=.. (data
(datalevel)
level)
Loop
Loop
++ xx Load
Load Very
Veryfine
finegrain
grain
(multiple
(multipleissue)
issue)
With hardware6
Multithreading
Multithreading --
Uniprocessors
Uniprocessors

 Concurrency Vs
Concurrency Vs Parallelism
Parallelism
 Concurrency
Concurrency

P1
P1

P2
P2 CPU

P3
P3

tim
tim
ee

Number
Number of
of Simulatneous
Simulatneous execution
execution units
units >
> no
no
of
of CPUs
CPUs 7
Multithreading
Multithreading --
Multiprocessors
Multiprocessors

Concurrency
Concurrency Vs
Vs Parallelism
Parallelism
CPU

P1
P1
CPU
P2
P2
CPU
P3
P3

tim
tim
ee
No
No of
of execution
execution process
process =
= no
no of
of CPUs
CPUs
8
Threads

 Java has built in thread support for


Multithreading
 Synchronization
 Thread Scheduling
 Inter-Thread Communication:
currentThread start setPriority
yield run getPriority
sleep stop suspend
resume
 Java Garbage Collector is a low-priority
thread

9
Threading Mechanisms...
 Create a class that extends the Thread class
 Create a class that implements the Runnable
interface

10
1st method: Extending Thread
class
 1st Method: Extending the Thread class
class MyThread extends Thread
{
public void run()
{
// thread body of execution
}
}
 Creating thread:
MyThread thr1 = new MyThread();
 Start Execution:
thr1.start();

11
2nd method: Threads by implementing
Runnable interface
class ClassName implements Runnable
{
.....
public void run()
{
// thread body of execution
}
}
 Creating Object:

ClassName myObject = new ClassName();


 Creating Thread Object:

Thread thr1 = new Thread( myObject );


 Start Execution:

thr1.start();

12
Multi-Threaded

 Thread and its subclasses run one thread


per instance
 class MyThread extends Thread { public
void run () { ...Start running } }
 Any Runnable object can wrap a thread
around itself
 class Server implements Runnable { Thread
t; Server () { t = new Thread(this); .}
 public void run () { ...Start running } }
 Thread control: Thread.start(),
Thread.stop()

13
Manipulation of Current Thread

// CurrentThreadDemo.java
class CurrentThreadDemo {
public static void main(String arg[]) {
Thread ct = Thread.currentThread();
ct.setName( "My Thread" );
System.out.println("Current Thread : "+ct);
try {
for(int i=5; i>0; i--) {
System.out.println(" " + i);
Thread.sleep(1000);
}
}
catch(InterruptedException e) {
System.out.println("Interrupted."); }
}
}
Run:
Current Thread : Thread[My Thread,5,main]
5
4
3
2
1

14
Creating new Thread...

// ThreadDemo.java
class ThreadDemo implements Runnable
{
ThreadDemo()
{
Thread ct = Thread.currentThread();
System.out.println("Current Thread : "+ct);
Thread t = new Thread(this,"Demo Thread");
t.start();
try
{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted.");
}
System.out.println("Exiting main thread.");
}

15
...Creating new Thread.

public void run() {


try {
for(int i=5; i>0; i--) {
System.out.println(" " + i);
Thread.sleep(1000);
} }
catch(InterruptedException e)
{ System.out.println("Child interrupted."); }
System.out.println("Exiting child thread.");
}
public static void main(String args[]) {
new ThreadDemo();
}
}
Run:
Current Thread : Thread[main,5,main]
5
4
3
Exiting main thread.
2
1
Exiting child thread.

16
Thread Priority...

// HiLoPri.java
class Clicker implements Runnable {
int click = 0;
private Thread t;
private boolean running = true;
public Clicker(int p)
{
t = new Thread(this);
t.setPriority(p);
}
public void run()
{
while(running)
click++;
}
public void start()
{ t.start(); }
public void stop()
{ running = false; }
}

17
...Thread Priority

class HiLoPri
{
public static void main(String args[])
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Clicker Hi = new Clicker(Thread.NORM_PRIORITY+2);
Clicker Lo = new Clicker(Thread.NORM_PRIORITY-2);
Lo.start();
Hi.start();
try { Thread.sleep(10000); }
catch (Exception e) { }
Lo.stop();
Hi.stop();
System.out.println(Lo.click + " vs. " + Hi.click);
}
}
Run1: (on Solaris)
0 vs. 956228
Run2: (Window 95)
304300 vs. 4066666

18
Threads Need
Synchronization

 Server instances lock themselves at method-


level
 class Buffer { // synchronize on buffer
instance public synchronized void put (int i) ...
 public synchronized int get () . void safe () .}
 Client instance locks a server object before
using it
 synchronized (array_to_sort) { ..... }wait(); //
within synchronized methodnotify();
 notifyall(); // wake up from a wait

19
Monitor model (for
Syncronisation)

Method 1

Method 2

Key
Block 1

Threads

Monitor (synchronised) solves race-condition problem


20
Threads Synchronisation...

// Synch.java: race-condition without synchronisation


class Callme {
// Check synchronized and unsynchronized methods
/* synchronized */ void call(String msg)
{
System.out.print("["+msg);
try { Thread.sleep(1000); }
catch(Exception e) { }
System.out.println("]");
}
}
class Caller implements Runnable
{
String msg;
Callme Target;
public Caller(Callme t, String s)
{
Target = t;
msg = s;
new Thread(this).start();
}

21
...Threads Synchronisation.

public void run() {


Target.call(msg);
}
}
class Synch {
public static void main(String args[]) {
Callme Target = new Callme();
new Caller(Target, "Hello");
new Caller(Target, "Synchronized");
new Caller(Target, "World");
}
}
Run 1: With unsynchronized call method (race condition)
[Hello[Synchronized[World]
]
]
Run 2: With synchronized call method
[Hello]
[Synchronized]
[World]
Run3:Synchronized object in run(): synchronized(Target){ Target.call(msg); }
The output is the same as Run2 22
Queue (no inter-threaded
communication)...
// pc.java: produce and consumer
class Queue
{ int n;
synchronized int get()
{
System.out.println("Got : "+n);
return n;
}
synchronized void put(int n)
{ this.n = n;
System.out.println("Put : "+n);
}
}
class Producer implements Runnable
{
Queue Q;
Producer(Queue q)
{
Q = q;
new Thread( this, "Producer").start();
}

23
Queue (no inter-threaded
communication)...

public void run()


{
int i = 0;
while(true)
Q.put(i++);
}
}
class Consumer implements Runnable
{
Queue Q;
Consumer(Queue q)
{
Q = q;
new Thread( this, "Consumer").start();
}
public void run()
{
while(true)
Q.get();
}
}

24
...Queue (no inter-threaded
communication).

class PC
{
public static void main(String[] args)
{
Queue Q = new Queue();
new Producer(Q);
new Consumer(Q);
}
}
Run:
Put: 1
Got: 1
Got: 1
Got: 1
Put: 2
Put: 3
Got: 3
^C

25
Queue (interthread communication)...

// PCnew.java: produce-consumenr with interthread communication


class Queue
{
int n;
boolean ValueSet = false;
synchronized int get()
{
try
{
if(!ValueSet)
wait();
}
catch(InterruptedException e)
{
}
System.out.println("Got : "+n);
ValueSet = false;
notify();
return n;
}
26
Queue (interthread communication)...

synchronized void put(int n)


{
try {
if(ValueSet)
wait();
}
catch(InterruptedException e) { }
this.n = n;
System.out.println("Put : "+n);
ValueSet = true;
notify();
}
}
class Producer implements Runnable
{
Queue Q;
Producer(Queue q)
{
Q = q;
new Thread( this, "Producer").start();
}

27
Queue (interthread communication)...

public void run()


{
int i = 0;
while(true)
Q.put(i++);
}
}
class Consumer implements Runnable
{
Queue Q;
Consumer(Queue q)
{
Q = q;
new Thread( this, "Consumer").start();
}
public void run()
{
while(true)
Q.get();
}
}

28
...Queue (no interthread
communication).

class PCnew
{
public static void main(String[] args)
{
Queue Q = new Queue();
new Producer(Q);
new Consumer(Q);
}
}
Run:
Put : 0
Got : 0
Put : 1
Got : 1
Put : 2
Got : 2
Put : 3
Got : 3
Put : 4
Got : 4
^C

29
Deadlock...

// DeadLock.java
class A
{
synchronized void foo(B b)
{
String name = Thread.currentThread().getName();
System.out.println(name + " entered A.foo");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}
System.out.println(name + " trying to call B.last()");
b.last();
}
synchronized void last()
{
System.out.println("Inside A.last");
}
}

30
Deadlock...
class B
{
synchronized void bar(A a)
{
String name = Thread.currentThread().getName();
System.out.println(name + " entered B.bar");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}
System.out.println(name + " trying to call A.last()");
a.last();
}

synchronized void last()


{
System.out.println("Inside B.last");
}
}

31
...Deadlock.

class DeadLock implements Runnable {


A a = new A(); B b = new B();
DeadLock() {
Thread.currentThread().setName("Main Thread");
new Thread(this).start();
a.foo(b);
System.out.println("Back in the main thread.");
}
public void run() {
Thread.currentThread().setName("Racing Thread");
b.bar(a); System.out.println("Back in the other thread");
}
public static void main(String args[]) {
new DeadLock();
}
}
Run:
Main Thread entered A.foo
Racing Thread entered B.bar
Main Thread trying to call B.last()
Racing Thread trying to call A.last()
^C

32
Threads
Threads in
in Action...
Action...
Cooperative
Cooperative threads
threads -- File
File
Copy
Copy
reader()
reader()
{{ writer()
writer()
-- -- -- -- -- -- -- -- -- buff[0] {{
-- buff[0]
-- -- -- -- -- -- -- -- -- --
lock(buff[i]);
lock(buff[i]); lock(buff[i]);
buff[1] lock(buff[i]);
read(src,buff[i]);
read(src,buff[i]); buff[1] write(src,buff[i]);
write(src,buff[i]);
unlock(buff[i]);
unlock(buff[i]); unlock(buff[i]);
unlock(buff[i]);
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- }}
}}

Cooperative
Cooperative Parallel
Parallel
Synchronized
Synchronized Threads
Threads
33
Threads in Action...
Multithreaded Server

Server Process
Client
Process Server
Threads

Client Process

User Mode

Kernel Mode
Message Passing
Facility

34

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