Chapter 2 Multithreading - Minh
Chapter 2 Multithreading - Minh
2
Review
3
Review
4
Review
● Class Declaration:
[public] class ClassName [extends BaseClass] [implements Interface] {
[modifiers] DataType field1 [ = initialValue];
[modifier] ClassName (params) { // constructor
<code>
}
[modifier] methodName(params) { // method
<code>
}
}
● Creating objects: ClassName obj = new ClassName(params)
● Accessing an object: obj.field or obj.methodeName(args)
5
Objectives
● Multi-Processing System
● Threads and Multi-Threading Program
● Thread Fundamentals in Java
6
1- Processes and
Multi-Processing System
7
Processes and Multi Processing System…
p3
p2
p1
t
10
Processes and Multi Processing System…
How can OS manage concurrent processes?
12
Threads and Multi-Threading …
How can JVM manage threads
● Thread is a smallest unit code in an
application that performs a special job.
➔ A program can have several threads they
can be executed concurrently. App
Data
Thread Table maintains information of threads
… … … … …. Memory of a
multi-thread process
13 Time-slicing mechanism is used to
schedule thread executions also.
Threads and Multi-Threading …
Processes VS Threads
App1
App1
Data
data
Code
Code
App2 Thread1
Data
Code Thread2
App3 Thread3
Data
Code
Memory Memory
Protection mechanism in Threads in a process
OS does not allow this can access common
process accessing data of this process
14 addresses in others
Threads and Multi-Threading …
Race Conditions
Thread 4
A race Need
Synchronization
occurs
15
3- Thread Fundamentals in Java
16
Create a subclass of the Thread class
Thread Code Addr State …
t 10320 run
?
This process has 2 threads running
The start() method is implemented in the concurrently. At a time, order of
Thread class. This method calls the their execution is decided by the
17 method run(). scheduler.
Class implements the Runnable interface
18
The java.lang.Thread class
Declaration
public class Thread extends Object implements Runnable
set/get-is Properties
id Common Methods
Constructor
name start()
Thread()
state join ()
Thread(Runnable target) sleep (milisec)
Thread(Runnable target, String name) threadGroup
yield()
Thread(String name) daemon notify()
Thread(ThreadGroup group, Runnable target) priority notifyAll()
wait()
Thread(ThreadGroup group, Runnable target, String name)
Thread(ThreadGroup group, Runnable target, String name,
long stackSize)
Thread(ThreadGroup group, String name)
19
Using some methods of the Thread class
20
Running
Thread States
Blocking
wait() method
sleep(millisecond) (IO)
Ready: As soon as it is created , it can enter the running state when JVM’s processor
is assigned to it.
Running: It get full attention of JVM’s processor which executes the thread’s run()
method
21
Dead: When the run() method terminates.
Summary
22
Review
23
Objectives
24
Non-race Demonstration
25
Non-race Demonstration…
26
4- Monitors, Waiting and Notifying
Producer Consumer
Attention!:
Store is common resource of 2 threads: producer and consumer.
➔ Store is a monitor and it’s activities needs synchronization
Synchronizing:
* After a thread accessed common resource, it should sleep a moment or it must notify to the
next thread ( or all thread) in the thread-pool to awake and execute.
* Use the synchronized keyword to declare a method that will access common resource.
29
The Producer-Consumer Problem
/* synchronized */
No synchronization
30
The Producer-Consumer Problem
31
The Producer-Consumer Problem
32
The Producer-Consumer Problem
Synchronization is not used Synchronization is used:
33
5 - Deadlock
What is deadlock?
Deadlock describes a situation
where two or more threads are
blocked forever, waiting for
each other → All threads in a
group halt.
When does deadlock occur?
There exists a circular wait
the lock that is held by other
thread.
Nothing can ensure that DEADLOCK
do not occur.
34
Deadlock Demo.
35
The Philosophers Problem
Wait-Notify
Mechanism, a way
helps preventing
deadlocks
Deadlock!
3 classes
36
The Philosophers Problem
Thread table
38
The Philosophers Problem
39
The Philosophers Problem
40
Summary
● Multi-processing system
mechanism.
● If you want some tasks executing concurrently,
multi-threading is a solution.
41
Exercises
42
Thank You