0% found this document useful (0 votes)
44 views

Chapter 2 Multithreading - Minh

This document discusses multi-threading and threads in Java. It explains that multi-threading allows multiple tasks to run concurrently within a program. Threads are lightweight processes that exist within a process and share its resources. The Java platform supports multi-threading through the Thread class and Runnable interface. Threads can be created by extending Thread or implementing Runnable.
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)
44 views

Chapter 2 Multithreading - Minh

This document discusses multi-threading and threads in Java. It explains that multi-threading allows multiple tasks to run concurrently within a program. Threads are lightweight processes that exist within a process and share its resources. The Java platform supports multi-threading through the Thread class and Runnable interface. Threads can be created by extending Thread or implementing Runnable.
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
You are on page 1/ 43

Lecture : Multi-Threading

Why should you study this chapter?

● This chapter will help you developing a program in


which some tasks executing concurrently.
● Nowadays, in one program, some tasks execute
concurrently. You usually saw them in a web page
including texts, sounds, images, games, … changing
concurrently.
● Nowadays, operating systems (OS) support many
programs running concurrently.
● Open the Task Manager of Windows OS (Ctrl Alt
Delete) to see how many programs are running in your
computer.

2
Review

Concepts introduced in the course OOP using Java:


● Object = properties + methods
● Benefits of OO Implementation: encapsulation,
inheritance, polymorphism
● Encapsulation is implemented by modifiers
● Inheritance: Subclass inherits declaration of base class
(Java is a single inheritance OOP language, the class
Object is the ultimate Java class
● Polymorphism in Java is implemented by overloading
and overriding methods

3
Review

● Class: A template (blueprint) for a group of similar


instances
● Interface: the core of some classes
● Abstract class is a class containing abstract methods
(some methods are prototyped only)
● Anonymous class: A concrete class developed from an
abstract class or an interface but it is not named:
● Nested class: a class which is declared in the
declaration of the enclosing class
● Enum type: Declaration of some constants

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

● Program: An executable file (data + Data


Code
P1
code) stored in secondary
memory(disk). Data
● Process: A program in running. It’s Code
P2
data and code are loaded to RAM
in a contiguous memory block. Data
Code
P3
● Multi-Processing System: An
operating system allows some Data
Code
P4
processes running concurrently

7
Processes and Multi Processing System…

● A process has a self-contained execution environment. A


process generally has a complete, private set of basic run-
time resources; in particular, each process has its own
memory space.
● Multi Processing/ Multi Tasking System: Almost of operating
systems allows many processes executing concurrently.
● Open the Task Manager of Windows OS ( Ctrl+Alt+Del) to
see current processes in your computer.
● Applications tag contains processes which you start them.
● Processes tag contains processes which automatically
run immediately after the startup of OS.
8
Processes and Multi Processing System…

CPU 2 cores → 1 core runs 33 (66/2)


processes
➔ Pseudo -parallelism
9
Processes and Multi Processing System…

● How OS manages processes based on one CPU?


● Time-sharing mechanism: OS allows each
process running in a time slot (time slice,
quantum, about 50 to 70 ms). When this duration
expires, another process will be chosen to run →
The scheduler (a component of OS, trình lập lịch)
will choose the current process.

p3
p2
p1
t
10
Processes and Multi Processing System…
How can OS manage concurrent processes?

Process Table maintains information of processes.


P1
App Code Addr Duration CPU …
Code
(mili sec)
Data
P1 30320 50 1
P2 20154 60 2
P2
Code P3 10166 70 1
Data
… … … …
… … … …
P3
Code
Data
Time-slicing mechanism. Each process is allocated resources (
CPU, …) for executing in a time-slot (such as 50 milliseconds).
Memory When the time duration expires, this process will pause to yield
resources to the next process which will be chosen by the
11 scheduler of OS.
2- Threads and Multi-Threading

● Thread, is sometimes called lightweight processes, is a


running code unit.
● Threads exist within a process — every process has at
least one thread (main thread). Threads share the
process's resources, including memory and open files. This
makes for efficient, but potentially problematic,
communication.
● Both processes and threads are provided an execution
environment, but creating a new thread requires fewer
resources than creating a new process.
● Multithreaded execution is an essential feature of the Java
platform. Threads in a program are managed by the JVM.
Scheduler in JVM will choose a current thread.

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

Thread Code Duration CPU State Code of thread1


Addr (mili
sec) Code of thread2
Thread 1 10320 15 1 ready
Code of thread3
Thread 2 40154 17 2 ready

Thread 3 80166 22 1 sleep

… … … … …. 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

Application Suppose that Thread2 and Thread4 concurrently execute.

Data 1 (shared data) Time Thread 2 Thread4


1 Data1=10 …
Data 2
2 …. …
Data 3 3 …. Data1= 100
4 …. …
Thread 1
5 … Y=?…
Thread 2 6 Y= 2*Data1; …
7 …
Thread3

Thread 4
A race Need
Synchronization
occurs
15
3- Thread Fundamentals in Java

● Threading supports in Java:


● The java.lang.Thread class
● The java.lang.Object class
● The Java language and JVM ( Java Virtual Machine)
● How to create a thread in Java?
(1) Create a subclass of the java.lang.Thread class and
override the method run()
(2) Create a class implementing the Runnable interface and
override the run() method.

16
Create a subclass of the Thread class
Thread Code Addr State …

main 8000 run …

t 10320 run

Every process has at least


one thread (main thread).

?
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

Thread Code Addr State …

main 8000 run …


t 10320 run

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)

Monitor Suspended Sleeping Blocked


States yield()
(JVM) start() Time
Blocking condition
(Scheduler) expired or
changes or interrupt
interrupted
notify()

Ready Interrupt: A signal is sent to CPU from IO


device just after an IO operation has
Only one of ready threads will be terminated.
chosen by the JVM scheduler at a
time.

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

Concepts were introduced:


● Definitions: Program, Process, Thread
● Multi-processing system
● Multi-threading programming in Java
● Thread Fundamentals in Java
● Thread states

22
Review

In the previous session:


● Definitions: Program, Process, Thread
● Multi-processing system
● Multi-threading programming in Java
● Thread Fundamentals in Java
● Thread states

23
Objectives

● How to develop a non-race multi-thread


applications
● How to develop a multi-thread applications in
which some threads accessing common
resources?
● Contents:
● Demonstrations
● Monitors, Waiting and Notifying
● Deadlocks

24
Non-race Demonstration

This program contains 2 threads:


- The first thread that will print out the system time for every second.
- The second will print out the sum of two random integers for every
half of a second.

25
Non-race Demonstration…

26
4- Monitors, Waiting and Notifying

● Some threads can access common resources concurrently. We


must synchronize accessing common resources ➔ Every object
has a lock
● Lock: an extra variable is added by the compiler for monitoring the
state of common resource. Before a thread accesses the common
resource, the lock is tested.
● After a thread has the lock (it is permitted to access the common
resource), it can access common resource. When it did, it needs
notifying to others thread ( wait-notify mechanism).

Enter synchronized code


Running
notify(), wait()
Seeking notifyAll(),
waiting Scheduler
Lock time out, or
interrupt
Ready
Lock obtained
27
Monitors, Waiting and Notifying…

● Two ways to mark code as synchronize


● Synchronize an entire method: Let the synchronized
modifier in the method’s declaration. A class contains
synchronized Type Method(args){ synchronized code is
called monitor.
<code>
}
● Synchronize some method of an object
Type Method ( args){ Monitor: A technique to
encapsulate common
……
resources to a class.
synchronized ( object_var){ When a thread wants to
object_var.method1(args); access common resources,
object_var.method2(args); it must call a public
synchronized method. As a
} result, common resources
} are accessed in successive
manner.
28
Demo: The Producer-Consumer Problem

- Producer makes a product then puts it to a store.


- Consumer buys a product from a store.
- Selling Procedure: First In First Out

product Store product

Producer Consumer

Put to the tail Get from the head

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

A product is simulated as a number.

/* 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

Threa Code Duration CP State


d Addr (mili U
sec)
Thread 10320 15 1 Suspended
1 →Ready
Thread 40154 17 2 Suspended
2
Thread 80166 22 1 Suspended
3
37 … … … … ….
The Philosophers Problem

38
The Philosophers Problem

39
The Philosophers Problem

40
Summary

Concepts were introduced:


● Definitions: Program, Process, Thread

● Multi-processing system

● Multi-threading programming in Java

● Thread Fundamentals in Java

● Synchronizing access to common resource.

● Monitoring thread states: Wait-notify

mechanism.
● If you want some tasks executing concurrently,

multi-threading is a solution.
41
Exercises

Workshop 1 (with report): The producer-


consumer problem and
Workshop 2 (with report): the philosophers
problem.

42
Thank You

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