This document outlines the key concepts and objectives covered in Lecture 04 on threads. It introduces processes and threads, defining them as the basic units of execution in operating systems and Java programs respectively. It discusses how multi-processing systems and multi-threading programs allow concurrent execution. The lecture then covers thread fundamentals in Java, including creating and managing threads, and synchronizing access to shared resources using monitors, locks, and wait-notify mechanisms. Students are assigned workshops on the producer-consumer problem and philosophers problem to demonstrate these concurrency concepts.
University of Virginia
cs4414: Operating Systems
http://rust-class.org
For embedded notes, see:
http://rust-class.org/class-22-microkernels-and-beyond.html
This is the twelfth set of slightly updated slides from a Perl programming course that I held some years ago.
I want to share it with everyone looking for intransitive Perl-knowledge.
A table of content for all presentations can be found at i-can.eu.
The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse
cs4414: Operating Systems
http://rust-class.org/class-1-what-is-an-operating-system.html
Class 1: What is an Operating System?
Why so many programming languages?
Introducing Rust
University of Virginia
cs4414: Operating Systems
http://rust-class.org
For embedded notes, see:
http://rust-class.org/class-22-microkernels-and-beyond.html
This is the twelfth set of slightly updated slides from a Perl programming course that I held some years ago.
I want to share it with everyone looking for intransitive Perl-knowledge.
A table of content for all presentations can be found at i-can.eu.
The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse
cs4414: Operating Systems
http://rust-class.org/class-1-what-is-an-operating-system.html
Class 1: What is an Operating System?
Why so many programming languages?
Introducing Rust
These slides were presented during technical event at my organization. It focuses on overview to find a root cause of the unexpected system down events. It is mainly useful for Linux or Unix system administrators. Here, I tried to cover all aspects of the topic. It took me more than 2 hours to present these slides, but one can also cover these slides within short time-span. Gray background of slides is implemented to hide the company logo and to preserve the confidentially of private template. However, The Knowledge is not restricted :)
OS | Functions of OS | Operations of OS | Operations of a process | Scheduling algorithms | FCFS scheduling | SJF scheduling | RR scheduling | Paging | File system implementation | Cryptography as a security tool
Literature Review Basics and Understanding Reference Management.pptxDr Ramhari Poudyal
Three-day training on academic research focuses on analytical tools at United Technical College, supported by the University Grant Commission, Nepal. 24-26 May 2024
HEAP SORT ILLUSTRATED WITH HEAPIFY, BUILD HEAP FOR DYNAMIC ARRAYS.
Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to the selection sort where we first find the minimum element and place the minimum element at the beginning. Repeat the same process for the remaining elements.
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesChristina Lin
Traditionally, dealing with real-time data pipelines has involved significant overhead, even for straightforward tasks like data transformation or masking. However, in this talk, we’ll venture into the dynamic realm of WebAssembly (WASM) and discover how it can revolutionize the creation of stateless streaming pipelines within a Kafka (Redpanda) broker. These pipelines are adept at managing low-latency, high-data-volume scenarios.
A review on techniques and modelling methodologies used for checking electrom...nooriasukmaningtyas
The proper function of the integrated circuit (IC) in an inhibiting electromagnetic environment has always been a serious concern throughout the decades of revolution in the world of electronics, from disjunct devices to today’s integrated circuit technology, where billions of transistors are combined on a single chip. The automotive industry and smart vehicles in particular, are confronting design issues such as being prone to electromagnetic interference (EMI). Electronic control devices calculate incorrect outputs because of EMI and sensors give misleading values which can prove fatal in case of automotives. In this paper, the authors have non exhaustively tried to review research work concerned with the investigation of EMI in ICs and prediction of this EMI using various modelling methodologies and measurement setups.
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...ssuser7dcef0
Power plants release a large amount of water vapor into the
atmosphere through the stack. The flue gas can be a potential
source for obtaining much needed cooling water for a power
plant. If a power plant could recover and reuse a portion of this
moisture, it could reduce its total cooling water intake
requirement. One of the most practical way to recover water
from flue gas is to use a condensing heat exchanger. The power
plant could also recover latent heat due to condensation as well
as sensible heat due to lowering the flue gas exit temperature.
Additionally, harmful acids released from the stack can be
reduced in a condensing heat exchanger by acid condensation. reduced in a condensing heat exchanger by acid condensation.
Condensation of vapors in flue gas is a complicated
phenomenon since heat and mass transfer of water vapor and
various acids simultaneously occur in the presence of noncondensable
gases such as nitrogen and oxygen. Design of a
condenser depends on the knowledge and understanding of the
heat and mass transfer processes. A computer program for
numerical simulations of water (H2O) and sulfuric acid (H2SO4)
condensation in a flue gas condensing heat exchanger was
developed using MATLAB. Governing equations based on
mass and energy balances for the system were derived to
predict variables such as flue gas exit temperature, cooling
water outlet temperature, mole fraction and condensation rates
of water and sulfuric acid vapors. The equations were solved
using an iterative solution technique with calculations of heat
and mass transfer coefficients and physical properties.
2. 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
3. Review
Previous lecture:
Introduction to OO Paradigm
Benefits of OO Implementation
Hints for class design
Declaring/ Using classes/ abstract classes/
Interfaces/ Anonymous classes/ Enum types
3
4. Objectives
Definitions: Process, thread.
Multi-Processing System and Multi-Threading
Program.
What are threads.
Thread Fundamentals in Java
Monitors, Waiting and Notifying
Your homework:
Workshop 3 (with report): The producer-consumer
problem and the philosophers problem.
Workshop 4 (with report): Deadlock application. Refer
to the source code in the page 236 of the book
Complete Java 2 Certification. Explain why the
application causes deadlock.
Assessment: the next class.
4
5. 1- Definitions
Program-chương trình: An executable
file (data + code) stored in secondary
memory(disk).
Process- tiến trình: A program in
running. It’s data and code are loaded
to RAM in a contiguous memory block.
Thread- luồng: is a smallest unit code
(function/method) in an application that
performs a special job. An application
can contain some threads. A thread
can be called as lightweight process.
Data
Code
Thread 1
Thread 2
Thread 3
5
6. 7.1- 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.
6
7. How can OS manage concurrent
processes
Memory
App1
Code
Data
App2
Code
Data
App3
Code
Data
App Code
Addr
Duration
(mili
sec)
CP
U
…
App
1
10320 15 1
App
2
40154 17 2
App
3
80166 22 1
… … … …
… … … …
Process Table maintains information of
processes.
How does OS manage executions of processes?
Time-slicing mechanism. Each process is allocated
resources ( CPU, …) for executing in a time-slot
(such as 30 milliseconds). When the time duration
expires, this process will pause to yield resources to
the next process which will be chosen by the
scheduler of OS.7
8. 7.2- Threads and Multi-Threading
Threads are sometimes called lightweight processes.
Both processes and threads provide an execution
environment, but creating a new thread requires fewer
resources than creating a new process.
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.
Multithreaded execution is an essential feature of the
Java platform. Threads in a program are managed by
the JVM.
8
9. How can JVM manage threads
Memory of a
multi-thread process
App
Data
Code of thread1
Code of thread2
Code of thread3
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.
Thread Code
Addr
Duratio
n
(mili
sec)
CPU State
Thread
1
10320 15 1 ready
Thread
2
40154 17 2 ready
Thread
3
80166 22 1 sleep
Thread Table maintains information of
threads
Time-slicing mechanism is used
to schedule thread executions
also.
9
11. Race Conditions
Application
Data 1 (shared
data)
Data 2
Data 3
Thread 1
Thread 2
Thread3
Thread 4 A
race
occur
s
Need
Synchronizatio
n
Suppose that Thread2 and Thread4
concurrently execute.
Time Thread 2 Thread4
1 Data1=10 …
2 …. …
3 …. Data1= 100
4 …. …
5 … …
6 Y= Data1; …
7 …
Y=
?
11
12. 7.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.
12
13. Threa
d
Code
Addr
State …
main 8000 run …
t 10320 run
Create a subclass of the Thread class
Thread table
Every process has
at least one thread
(main thread).
?
The start() method is implemented in the
Thread class. This method calls the
method run().
This process has 2 threads
running concurrently. At a time,
order of their execution is
decided by the scheduler.
13
14. Class implements the Runnable interface
Threa
d
Code
Addr
State …
main 8000 run …
t 10320 run
14
15. The java.lang.Thread class
Constructor
Thread()
Thread(Runnable target)
Thread(Runnable target, String name)
Thread(String name)
Thread(ThreadGroup group, Runnable target)
Thread(ThreadGroup group, Runnable target,
String name)
Thread(ThreadGroup group, Runnable target,
String name, long stackSize)
Thread(ThreadGroup group, String name)
Declaration
public class Thread extends Object implements Runnable
Common Methods
start()
join ()
sleep (milisec)
yield()
notify()
notifyAll()
wait()
Properties
id
name
state
threadGroup
daemon
priority
set/get-is
15
17. Thread States
Running
BlockedSleeping
Suspende
d
Ready
Monitor
States
(JVM)
yield()
start()
(Scheduler)
sleep(milisec)
Time expired
or interrupted
Blocking method
(IO)
Blocking condition
changes or
interrupt
wait()
notify()
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
Dead: When the run() method terminates.
Interrupt: A signal is sent to CPU from
IO device just after an IO operation has
terminated.Only ready threads will be
chosen by the JVM scheduler
at a time.
17
18. 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.
18
20. 7.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).
Running
Seeking
Lock
waiting
Ready
Scheduler
wait()notify(),
notifyAll(),
time out, or
interrupt
Enter synchronized
code
Lock obtained
20
21. Monitors, Waiting and Notifying…
Two ways to mark code as synchronize
Synchronize an entire method: Let the synchronized
modifier in the method’s declaration.
synchronized Type Method(args){
<code>
}
Synchronize some method of an object
Type Method ( args){
……
synchronized ( object_var){
object_var.method1(args);
object_var.method2(args);
}
}
A class
contains
synchronized
code is called
monitor.
Monitor: A technique to
encapsulate common
resources to a class.
When a thread wants to
access common
resources, it must call a
public synchronized
method. As a result,
common resources are
accessed in successive
manner.21
22. 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
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.
Producer
Consume
r
produc
t
produc
t
Put to the tail Get from the
head
Store
22
27. 7.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.
27
33. 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.
33
34. Exercises
Workshop 3 (with report): The
producer-consumer problem and the
philosophers problem.
Workshop 4 (with report): Deadlock
application. Refer to the source code in
the page 236 of the book Complete Java
2 Certification. Explain why the
application causes deadlock.
34
Synchronize –đồng(cùng) bộ(bước chân). Bản chất của sự đồng bộ là chờ nhau. Trong tin học, đồng bộ một nhóm tac vụ là cưỡng bức các tác vụ này phải thực thi tuần tự (không cho phép thực thi đồng thời). Giao tiếp đồng bộ là giao tiếp chỉ có một người nói tại một thời điểm (khi phỏng vấn, giao tiếp hình thức). Giao tiếp không đồng bộ là giao tiếp ở đó mọi người đều có thể nói đồng thời (khi cãi lộn).
join(): kết hợp Luồng hiện hành sẽ chạy cho đến khi luồng kết thúc
yield(): nhường Luồng hiện hành chủ động tạm ngưng, nhường CPU cho luồng khác
notify(): Cảnh báo Luồng hiện hành giao tiếp với JVM, yêu cầu JVM cho luồng ở đầu hàng đợi ready để khi lập lịch, luồng này sẽ được thực thi. Hành vi notify() thường được dùng ngay sau hành vi wait() JVM modifies thread table
notifyAll(): cảnh báo tất cả Luồng hiện hành yêu cầu JVM cho tất cả các luồng trong hàng đJVM modifies thread table