4-COE312 - Lecture Java Threads
4-COE312 - Lecture Java Threads
Java Threads
COE312
Omar Arif
2
Multitasking:
doing things
in parallel
4
Multiprocessing Multithreading
Each process have its
own address in
Threads share the
memory i.e. each
same address space.
process allocates
separate memory area.
Multitasking
5
Multiprocessing Multithreading
Each process have its
own address in
Threads share the
memory i.e. each
same address space.
process allocates
separate memory area.
Process is
Thread is lightweight.
heavyweight.
Multitasking
6
Multiprocessing Multithreading
Each process have its
own address in
Threads share the
memory i.e. each
same address space.
process allocates
separate memory area.
Process is
Thread is lightweight.
heavyweight.
Multitasking Cost of communication Cost of communication
between the process is between the thread is
high. low.
7
Multiprocessing Multithreading
Each process have its
own address in
Threads share the
memory i.e. each
same address space.
process allocates
separate memory area.
Process is
Thread is lightweight.
heavyweight.
Multitasking Cost of communication Cost of communication
between the process is between the thread is
high. low.
Context-switching
require some time for Context-switching
saving & loading between the threads
registers, memory takes less time than
maps, updating lists process.
etc.
8
Multithreadi
ng
9
• Thread is an execution unit.
• Your java program running inside a JVM
can have several threads at the same
time.
Your Java
Program
What is a
Java
Program
thread?
JVM
Computer
10
States of a Thread
12
<<interfacce>>
Thread
Runnable
Implements
+start() public void run();
<<interfacce>>
Thread
Runnable
Implements
+start() public void run();
<<interfacce>>
Thread
Runnable
Implements
+start() public void run();
start()
Allocates resources for a thread like
Key activation record stack.
Output:
These two instances of
Creating Thread 1
thread will run in parallel
Creating Thread 2
in addition to main.
Running Thread 1
Running Thread 2
Thread 1: 20
Thread 2: 17
Thread 1: 43
Thread 2: 90
Thread 2: 79
Both threads t1 and t2 run in parallel Thread 1: 77
to generate their own random
numbers.
Thread 2: 19
Thread 1: 1
Thread Thread 2 exiting.
Thread Thread 1 exiting.
import java.util.Random; 18
Print(int n){
this.n = n;
Example }
}}
24
Problem
with the Main has finished running Hasta Lavista Baby!
from:print@92bcdac print 1
Example
from:print@4c329cd1 print 1
from:print@92bcdac print 2
from:print@4c329cd1 print 2
n1 and n2 are still running from:print@92bcdac print 3
from:print@4c329cd1 print 3
from:print@92bcdac print 4
from:print@4c329cd1 print 4
25
}}
from:print@3edac053 print 7
public Thread getThread() {
from:print@54f50172 print 9
return t; from:print@3edac053 print 8
} from:print@3edac053 print 9
from:print@3edac053 print 10
from:print@54f50172 print 10
public void run() { from:print@3edac053 print 11
from:print@3edac053 print 12
for(int i =1; i<=n; i++)
from:print@3edac053 print 13
System.out.println("from:"+this+" print "+i); from:print@3edac053 print 14
} from:print@3edac053 print 15
Hasta Lavista Baby!
}
Common
Question
28
Example
32
}catch(Exception e) {
}
}
}
33
public class Door implements Runnable{ Create a thread and start onself.
public Thread t;
Counter counter;
Door(Counter c){
counter = c;
t = new Thread(this);
Simulate 5 people entering the
t.start();
} door
door1.t.join();
door2.t.join();
Synchonizati
private int count;
Counter(){
count = 0;
on }
public synchronized void addCounter() {
try {
int val = count;
Thread.sleep(10);
count = val+1;
System.out.println("Counter: " +
Integer.toString(count));
}catch(Exception e) {
}
}
}
37
Block synchronized(counter) {
Critical Section
Synchonizati counter.addCounter();
on
}
} Output:
}
}
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
Counter: 6
Surround the code that Counter: 7
Counter: 8
accesses the shared object
Counter: 9
with the synchronized block. Counter: 10
total: 10
Write a program that has two classes for female 39
public class {
}
}
41
this.notebook = notebook;
this.pencil = pencil;
t = new Thread(this);
Female
t.start();
}
Students
public void run() {
while (true) {
// wait for the notebook to become available
synchronized (notebook) {
Running
female_student@28d01fb4 has the pencil.
female_student@28d01fb4 has pencil and notebook so they can write
female_student@28d01fb4 has the notebook.
students
female_student@226e0250 has the notebook.
female_student@226e0250 has the pencil.
female_student@226e0250 has pencil and notebook so they can write
female_student@226e0250 has the notebook.
female_student@226e0250 has the pencil.
43
and Female
for (int i = 0; i<10; i++)
new male_student(notebook, pencil);
}
Students
}
What
happened? female_student@226e0250 has the notebook and is waiting for the
pencil.
male_student@2ee6a5aa has the pencil and it waiting for the notebook.
Both will wait for each other for ever and this is called a Deadlock.
45
Deadlock
same order.
2. Don’t wait for ever but release a resource if not
availeble through timeout. This can be done in Java
using a more advanced concept called a Lock. We
will not cover this topic in our class but if you are
interested please see
(https://docs.oracle.com/javase/7/docs/api/java/util/
concurrent/locks/Lock.html)
46
Summary