SlideShare a Scribd company logo
www.SunilOS.com 1
www.sunilos.com
www.raystec.com
Concurrency/Threads
Mother’s morning
www.SunilOS.com 2
Concurrency & Threads
1:
2:
3:
4:
6:
single threading
multi threading
1
Threading
www.SunilOS.com 3
Single Threading Multithreading
Thread
Thread
Thread
Thread
Thread
OS
OS
www.SunilOS.com 4
OS
CPU Cycle
Thread
Thread
Thread
Thread
Thread
Thread
Thread
HelloNoThread
1. public class HelloNoThread {
2. String name = null;
3. public HelloNoThread(String n) {
4. name = n;
5. }
6. public void run() {
7. for (int i = 0; i < 50; i++) {
8. System.out.println(i + name);
9. }
10. }
11.}
www.SunilOS.com 5
TestHelloNoThread
1. public class TestHelloNoThread {
2. public static void main(String[] args) {
3. HelloNoThread t1 = new HelloNoThread("Ram");
4. HelloNoThread t2 = new HelloNoThread("Shyam");
5. t1.run();
6. t2.run();
7. }
8. }
 0 Ram
 1 Ram
 2 Ram
 …..
 48 Ram
 49 Ram
 0 Shyam
 1 Shyam
 2 Shyam
 ……..
 48 Shyam
 49 Shyam
www.SunilOS.com 6
t1.run() t2.run()
Create Thread
www.SunilOS.com 7
Thread
+run()
+start()
+sleep()
MyThread HelloThread Other
+run() +run() +run()
extends
HelloThread
1. public class HelloThread extends Thread{
2. String name = null;
3. public HelloThread(String n) {
4. name = n;
5. }
6. public void run() {
7. for (int i = 0; i < 50; i++) {
8. System.out.println(i + name);
9. }
10. }
11.}
www.SunilOS.com 8
TestHelloThread
1. public class TestHelloThread{
2. public static void main(String[] args) {
3. HelloThread t1 = new HelloThread("Ram");
4. HelloThread t2 = new HelloThread("Shyam");
5. t1.start();
6. t2.start();
7. for (int i = 0; i < 50; i++) {
8. System.out.println(“main");
9. }
10. }
11.}
 0 Ram
 1 Ram
 2 Ram
 0 Shyam
 1 Shyam
 1 main
 2 main
 3 main
 3 Ram
 4 Ram
 2 Shyam
 3 Shyam
 4 main
 ……..
 48 Ram
 49 Ram
 48 Shyam
www.SunilOS.com 9
CPU Scheduling
CPU
Main
Ram
Shyam
T1 T2 T3
Pani puri –Thread scheduling
www.SunilOS.com 11
OS
CPU Cycle
Ready Thread Queue
Thread
Thread Thread Thread
Thread
Thread Priority
www.SunilOS.com 12
Priority range from 1-10
www.SunilOS.com 13
CPU Cycle
Priority
www.SunilOS.com 14
Set and get priority
 Thread t = new Thread(“Ram”);
o t.setPriority(9);
o int i = t.getPriority();
 Default priority is 5
 Priority constants are defined in Thread class:
o Thread.MIN_PRIORITY=1
o Thread.MAX_PRIORITY=10
o Thread.NORM_PRIORITY=5
Runnable interface
www.SunilOS.com 15
Runnable
+run()
Implements
ParentClass
extends
ChildClass
+run()
Runnable interface
1. public class HelloRunnable implements Runnable {
2. String name = null;
3. public HelloRunnable(String n) {
4. name = n;
5. }
6. public void run() {
7. for (int i = 0; i < 50; i++) {
8. System.out.println(i + name);
9. }
10. }
11.}
www.SunilOS.com 16
Test Runnable
 public class TestHelloRunnable {
 public static void main(String[] args) {
 Thread t1 = new Thread(new HelloRunnable("Ram"));
 Thread t2 = new Thread(new HelloRunnable("Shyam"));
 t1.start();
 t2.start();
 for (int i = 0; i < 500; i++) {
 System.out.println("Main");
 }
 }
 } www.SunilOS.com 17
www.SunilOS.com 18
Threads and Processes
Thread creation
A java program executed as thread inside JVM
A thread can create another thread.
Threads work concurrently.
If one thread dies, other will remain active in
memory
www.SunilOS.com 19
www.SunilOS.com 20
Thread Lifecycle - States
2: start()
finish run()
1: new
Born Blocked
Runnable
Dead
1. I/O available
2. notify()
3. wake up()
1. block on I/O
2. wait()
3. sleep()
www.SunilOS.com 21
Thread Methods
 start()
o Starts the thread and put into runnable queue
 run()
o Performs operation
 Thread.sleep(int m)/sleep(int m,int n)
o Sleeps the thread for given time in milliseconds and nanoseconds
 Thread.yield()
o Pauses currently executing thread and put at last in the runnable queue
www.SunilOS.com 22
Daemon Threads
 Daemon threads are supporting and background threads
 It provides services to normal threads
 Garbage Collector is a Daemon thread.
 t.setDaemon(true)
Daemon
Thread
Normal
Thread
Usage | multi user system
www.SunilOS.com 23
Web Application
Database
Race condition
www.SunilOS.com 24
Thread 1
Thread 2
Object
www.SunilOS.com 25
Race Condition
 Threads of a process can share common memory area
 When two threads simultaneously try to access and modify an
object, it is called race-condition
 The result of a program is affected by the order in which threads
are allocated CPU time and executed
www.SunilOS.com 26
Deposit in the Account
Account
Balance = 1000
Balance = 2000
getBalance():1000
setBalance(2000)
setBalance(2000)
getBalance():1000
getBalance()
CPU
getBalance()
setBalance()
setBalance()
User1 User 2
Deposit
Deposit
www.SunilOS.com 27
Synchronized access
Account
Balance = 1000
Balance = 3000
getBalance():1000
setBalance(2000)
setBalance(3000)
getBalance():2000
User1 User 2
Deposit Deposit
getBalance()
CPU
locked
setBalance()
setBalance()
getBalance()
www.SunilOS.com 28
Account
1. public class Account {
2. private int balance = 0;
3. public int getBalance() {
4. try {
5. Thread.sleep(200);
6. } catch (InterruptedException e) {}
7. return balance;
8. }
9. public void setBalance(int balance) {
10. try {
11. Thread.sleep(200);
12. } catch (InterruptedException e) {}
13. this.balance = balance;
14. }
15.
www.SunilOS.com 29
Account
1. public synchronized void deposit(String msg, int amt) {
2. int bal = getBalance();
3. bal = bal + amt;
4. setBalance(bal);
5. System.out.println(msg + " new balance " + bal);
6. }
7. }
www.SunilOS.com 30
RacingCondThread
public class RacingCondThread extends Thread {
public static Account data = new Account();
String name = null;
public RacingCondThread(String name) {
this.name = name;
}
public void run() {
for(int i=0;i<5;i++){
data.deposit(name, 1000);
}
}
www.SunilOS.com 31
TestRacingCondThread
public static void main(String[] args) {
RacingCondThread t1 = new RacingCondThread(“Ram");
RacingCondThread t2 = new RacingCondThread(“Shyam");
t1.start();
t2.start();
}
Output
 void deposit(..)
o Ram balance 1000
o Shyam balance 1000
o Ram balance 2000
o Shyam balance 2000
o Shyam balance 3000
o Ram balance 3000
o Shyam balance 4000
o Ram balance 4000
o Shyam balance 5000
o Ram balance 5000
 synchronized void deposit(..)
o Ram balance 1000
o Ram balance 2000
o Ram balance 3000
o Ram balance 4000
o Ram balance 5000
o Shyam balance 6000
o Shyam balance 7000
o Shyam balance 8000
o Shyam balance 9000
o Shyam balance 10000
www.SunilOS.com 32
www.SunilOS.com 33
Synchronization types
Method
public synchronized void deposit(String message, int amount)
{….}
Block
public void deposit(String message, int amount) {
synchronized (this){
int bal = getBalance() + amount;
setBalance(bal);
}
System.out.println(message + " Now Bal is " + bal);
}
www.SunilOS.com 34
Monitors / Locks
 It is a lock or token of an object
 The thread has lock, can access object
 Other threads will be in waiting queue
 One object has two monitors or tokens
o One for instance methods, called Object Monitor
o Second for static methods, called Class Monitor
www.SunilOS.com 35
Monitor Keys
Static Sync Methods
x
z
y
Instance Sync Methods
a
c
b
Thread1
Thread2
x() Class
Monitor
y()
a() Object
Monitor
b()
CLASS
Disclaimer
This is an educational presentation to enhance the skill
of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are used in
this presentation to simplify technical examples and
correlate examples with the real world.
We are grateful to owners of these URLs and pictures.
www.SunilOS.com 36
Thank You!
www.SunilOS.com 37
www.SunilOS.com

More Related Content

What's hot

Hibernate
Hibernate Hibernate
Hibernate
Sunil OS
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
Sunil OS
 
JavaScript
JavaScriptJavaScript
JavaScript
Sunil OS
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
Sunil OS
 
Resource Bundle
Resource BundleResource Bundle
Resource Bundle
Sunil OS
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
Sunil OS
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
Sunil OS
 
JUnit 4
JUnit 4JUnit 4
JUnit 4
Sunil OS
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
Sunil OS
 
Java Basics
Java BasicsJava Basics
Java Basics
Sunil OS
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
Sunil OS
 
Python Part 1
Python Part 1Python Part 1
Python Part 1
Sunil OS
 
DJango
DJangoDJango
DJango
Sunil OS
 
Machine learning ( Part 3 )
Machine learning ( Part 3 )Machine learning ( Part 3 )
Machine learning ( Part 3 )
Sunil OS
 
PDBC
PDBCPDBC
PDBC
Sunil OS
 
Machine learning ( Part 1 )
Machine learning ( Part 1 )Machine learning ( Part 1 )
Machine learning ( Part 1 )
Sunil OS
 
C++
C++C++
Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1
Sunil OS
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
Jeevesh Pandey
 
Optional in Java 8
Optional in Java 8Optional in Java 8
Optional in Java 8
Richard Walker
 

What's hot (20)

Hibernate
Hibernate Hibernate
Hibernate
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
Resource Bundle
Resource BundleResource Bundle
Resource Bundle
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
JUnit 4
JUnit 4JUnit 4
JUnit 4
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
Python Part 1
Python Part 1Python Part 1
Python Part 1
 
DJango
DJangoDJango
DJango
 
Machine learning ( Part 3 )
Machine learning ( Part 3 )Machine learning ( Part 3 )
Machine learning ( Part 3 )
 
PDBC
PDBCPDBC
PDBC
 
Machine learning ( Part 1 )
Machine learning ( Part 1 )Machine learning ( Part 1 )
Machine learning ( Part 1 )
 
C++
C++C++
C++
 
Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Optional in Java 8
Optional in Java 8Optional in Java 8
Optional in Java 8
 

Similar to Threads V4

JAVA THREADS.pdf
JAVA THREADS.pdfJAVA THREADS.pdf
JAVA THREADS.pdf
Mohit Kumar
 
Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)
Alexey Fyodorov
 
13multithreaded Programming
13multithreaded Programming13multithreaded Programming
13multithreaded Programming
Adil Jafri
 
Core Java Programming Language (JSE) : Chapter XII - Threads
Core Java Programming Language (JSE) : Chapter XII -  ThreadsCore Java Programming Language (JSE) : Chapter XII -  Threads
Core Java Programming Language (JSE) : Chapter XII - Threads
WebStackAcademy
 
Multi Threading
Multi ThreadingMulti Threading
Thread model in java
Thread model in javaThread model in java
Thread model in java
AmbigaMurugesan
 
unit-3java.pptx
unit-3java.pptxunit-3java.pptx
unit-3java.pptx
sujatha629799
 
Threads
ThreadsThreads
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance Puzzlers
Doug Hawkins
 
The Ring programming language version 1.10 book - Part 35 of 212
The Ring programming language version 1.10 book - Part 35 of 212The Ring programming language version 1.10 book - Part 35 of 212
The Ring programming language version 1.10 book - Part 35 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 25 of 180
The Ring programming language version 1.5.1 book - Part 25 of 180The Ring programming language version 1.5.1 book - Part 25 of 180
The Ring programming language version 1.5.1 book - Part 25 of 180
Mahmoud Samir Fayed
 
Threads
ThreadsThreads
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
PragatiSutar4
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Introduction to Rust language programming
Introduction to Rust language programmingIntroduction to Rust language programming
Introduction to Rust language programming
Rodolfo Finochietti
 
The Ring programming language version 1.7 book - Part 30 of 196
The Ring programming language version 1.7 book - Part 30 of 196The Ring programming language version 1.7 book - Part 30 of 196
The Ring programming language version 1.7 book - Part 30 of 196
Mahmoud Samir Fayed
 
Threads v3
Threads v3Threads v3
Threads v3
Sunil OS
 
Java Language fundamental
Java Language fundamentalJava Language fundamental
Java Language fundamental
Infoviaan Technologies
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
Seok-joon Yun
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
myrajendra
 

Similar to Threads V4 (20)

JAVA THREADS.pdf
JAVA THREADS.pdfJAVA THREADS.pdf
JAVA THREADS.pdf
 
Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)
 
13multithreaded Programming
13multithreaded Programming13multithreaded Programming
13multithreaded Programming
 
Core Java Programming Language (JSE) : Chapter XII - Threads
Core Java Programming Language (JSE) : Chapter XII -  ThreadsCore Java Programming Language (JSE) : Chapter XII -  Threads
Core Java Programming Language (JSE) : Chapter XII - Threads
 
Multi Threading
Multi ThreadingMulti Threading
Multi Threading
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 
unit-3java.pptx
unit-3java.pptxunit-3java.pptx
unit-3java.pptx
 
Threads
ThreadsThreads
Threads
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance Puzzlers
 
The Ring programming language version 1.10 book - Part 35 of 212
The Ring programming language version 1.10 book - Part 35 of 212The Ring programming language version 1.10 book - Part 35 of 212
The Ring programming language version 1.10 book - Part 35 of 212
 
The Ring programming language version 1.5.1 book - Part 25 of 180
The Ring programming language version 1.5.1 book - Part 25 of 180The Ring programming language version 1.5.1 book - Part 25 of 180
The Ring programming language version 1.5.1 book - Part 25 of 180
 
Threads
ThreadsThreads
Threads
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
Introduction to Rust language programming
Introduction to Rust language programmingIntroduction to Rust language programming
Introduction to Rust language programming
 
The Ring programming language version 1.7 book - Part 30 of 196
The Ring programming language version 1.7 book - Part 30 of 196The Ring programming language version 1.7 book - Part 30 of 196
The Ring programming language version 1.7 book - Part 30 of 196
 
Threads v3
Threads v3Threads v3
Threads v3
 
Java Language fundamental
Java Language fundamentalJava Language fundamental
Java Language fundamental
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 

More from Sunil OS

OOP v3
OOP v3OOP v3
OOP v3
Sunil OS
 
Exception Handling v3
Exception Handling v3Exception Handling v3
Exception Handling v3
Sunil OS
 
Machine learning ( Part 2 )
Machine learning ( Part 2 )Machine learning ( Part 2 )
Machine learning ( Part 2 )
Sunil OS
 
Python Pandas
Python PandasPython Pandas
Python Pandas
Sunil OS
 
Angular 8
Angular 8 Angular 8
Angular 8
Sunil OS
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
Sunil OS
 
C# Basics
C# BasicsC# Basics
C# Basics
Sunil OS
 
Rays Technologies
Rays TechnologiesRays Technologies
Rays Technologies
Sunil OS
 
C++ oop
C++ oopC++ oop
C++ oop
Sunil OS
 
C Basics
C BasicsC Basics
C Basics
Sunil OS
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
Sunil OS
 

More from Sunil OS (11)

OOP v3
OOP v3OOP v3
OOP v3
 
Exception Handling v3
Exception Handling v3Exception Handling v3
Exception Handling v3
 
Machine learning ( Part 2 )
Machine learning ( Part 2 )Machine learning ( Part 2 )
Machine learning ( Part 2 )
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Angular 8
Angular 8 Angular 8
Angular 8
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Rays Technologies
Rays TechnologiesRays Technologies
Rays Technologies
 
C++ oop
C++ oopC++ oop
C++ oop
 
C Basics
C BasicsC Basics
C Basics
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
 

Recently uploaded

Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
Prof. Dr. K. Adisesha
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
National Information Standards Organization (NISO)
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
khuleseema60
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
Celine George
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
David Douglas School District
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
heathfieldcps1
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
National Information Standards Organization (NISO)
 

Recently uploaded (20)

Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
 

Threads V4

  • 2. Mother’s morning www.SunilOS.com 2 Concurrency & Threads 1: 2: 3: 4: 6: single threading multi threading 1
  • 3. Threading www.SunilOS.com 3 Single Threading Multithreading Thread Thread Thread Thread Thread OS OS
  • 5. HelloNoThread 1. public class HelloNoThread { 2. String name = null; 3. public HelloNoThread(String n) { 4. name = n; 5. } 6. public void run() { 7. for (int i = 0; i < 50; i++) { 8. System.out.println(i + name); 9. } 10. } 11.} www.SunilOS.com 5
  • 6. TestHelloNoThread 1. public class TestHelloNoThread { 2. public static void main(String[] args) { 3. HelloNoThread t1 = new HelloNoThread("Ram"); 4. HelloNoThread t2 = new HelloNoThread("Shyam"); 5. t1.run(); 6. t2.run(); 7. } 8. }  0 Ram  1 Ram  2 Ram  …..  48 Ram  49 Ram  0 Shyam  1 Shyam  2 Shyam  ……..  48 Shyam  49 Shyam www.SunilOS.com 6 t1.run() t2.run()
  • 7. Create Thread www.SunilOS.com 7 Thread +run() +start() +sleep() MyThread HelloThread Other +run() +run() +run() extends
  • 8. HelloThread 1. public class HelloThread extends Thread{ 2. String name = null; 3. public HelloThread(String n) { 4. name = n; 5. } 6. public void run() { 7. for (int i = 0; i < 50; i++) { 8. System.out.println(i + name); 9. } 10. } 11.} www.SunilOS.com 8
  • 9. TestHelloThread 1. public class TestHelloThread{ 2. public static void main(String[] args) { 3. HelloThread t1 = new HelloThread("Ram"); 4. HelloThread t2 = new HelloThread("Shyam"); 5. t1.start(); 6. t2.start(); 7. for (int i = 0; i < 50; i++) { 8. System.out.println(“main"); 9. } 10. } 11.}  0 Ram  1 Ram  2 Ram  0 Shyam  1 Shyam  1 main  2 main  3 main  3 Ram  4 Ram  2 Shyam  3 Shyam  4 main  ……..  48 Ram  49 Ram  48 Shyam www.SunilOS.com 9
  • 11. Pani puri –Thread scheduling www.SunilOS.com 11 OS CPU Cycle Ready Thread Queue Thread Thread Thread Thread Thread
  • 13. Priority range from 1-10 www.SunilOS.com 13 CPU Cycle Priority
  • 14. www.SunilOS.com 14 Set and get priority  Thread t = new Thread(“Ram”); o t.setPriority(9); o int i = t.getPriority();  Default priority is 5  Priority constants are defined in Thread class: o Thread.MIN_PRIORITY=1 o Thread.MAX_PRIORITY=10 o Thread.NORM_PRIORITY=5
  • 16. Runnable interface 1. public class HelloRunnable implements Runnable { 2. String name = null; 3. public HelloRunnable(String n) { 4. name = n; 5. } 6. public void run() { 7. for (int i = 0; i < 50; i++) { 8. System.out.println(i + name); 9. } 10. } 11.} www.SunilOS.com 16
  • 17. Test Runnable  public class TestHelloRunnable {  public static void main(String[] args) {  Thread t1 = new Thread(new HelloRunnable("Ram"));  Thread t2 = new Thread(new HelloRunnable("Shyam"));  t1.start();  t2.start();  for (int i = 0; i < 500; i++) {  System.out.println("Main");  }  }  } www.SunilOS.com 17
  • 19. Thread creation A java program executed as thread inside JVM A thread can create another thread. Threads work concurrently. If one thread dies, other will remain active in memory www.SunilOS.com 19
  • 20. www.SunilOS.com 20 Thread Lifecycle - States 2: start() finish run() 1: new Born Blocked Runnable Dead 1. I/O available 2. notify() 3. wake up() 1. block on I/O 2. wait() 3. sleep()
  • 21. www.SunilOS.com 21 Thread Methods  start() o Starts the thread and put into runnable queue  run() o Performs operation  Thread.sleep(int m)/sleep(int m,int n) o Sleeps the thread for given time in milliseconds and nanoseconds  Thread.yield() o Pauses currently executing thread and put at last in the runnable queue
  • 22. www.SunilOS.com 22 Daemon Threads  Daemon threads are supporting and background threads  It provides services to normal threads  Garbage Collector is a Daemon thread.  t.setDaemon(true) Daemon Thread Normal Thread
  • 23. Usage | multi user system www.SunilOS.com 23 Web Application Database
  • 25. www.SunilOS.com 25 Race Condition  Threads of a process can share common memory area  When two threads simultaneously try to access and modify an object, it is called race-condition  The result of a program is affected by the order in which threads are allocated CPU time and executed
  • 26. www.SunilOS.com 26 Deposit in the Account Account Balance = 1000 Balance = 2000 getBalance():1000 setBalance(2000) setBalance(2000) getBalance():1000 getBalance() CPU getBalance() setBalance() setBalance() User1 User 2 Deposit Deposit
  • 27. www.SunilOS.com 27 Synchronized access Account Balance = 1000 Balance = 3000 getBalance():1000 setBalance(2000) setBalance(3000) getBalance():2000 User1 User 2 Deposit Deposit getBalance() CPU locked setBalance() setBalance() getBalance()
  • 28. www.SunilOS.com 28 Account 1. public class Account { 2. private int balance = 0; 3. public int getBalance() { 4. try { 5. Thread.sleep(200); 6. } catch (InterruptedException e) {} 7. return balance; 8. } 9. public void setBalance(int balance) { 10. try { 11. Thread.sleep(200); 12. } catch (InterruptedException e) {} 13. this.balance = balance; 14. } 15.
  • 29. www.SunilOS.com 29 Account 1. public synchronized void deposit(String msg, int amt) { 2. int bal = getBalance(); 3. bal = bal + amt; 4. setBalance(bal); 5. System.out.println(msg + " new balance " + bal); 6. } 7. }
  • 30. www.SunilOS.com 30 RacingCondThread public class RacingCondThread extends Thread { public static Account data = new Account(); String name = null; public RacingCondThread(String name) { this.name = name; } public void run() { for(int i=0;i<5;i++){ data.deposit(name, 1000); } }
  • 31. www.SunilOS.com 31 TestRacingCondThread public static void main(String[] args) { RacingCondThread t1 = new RacingCondThread(“Ram"); RacingCondThread t2 = new RacingCondThread(“Shyam"); t1.start(); t2.start(); }
  • 32. Output  void deposit(..) o Ram balance 1000 o Shyam balance 1000 o Ram balance 2000 o Shyam balance 2000 o Shyam balance 3000 o Ram balance 3000 o Shyam balance 4000 o Ram balance 4000 o Shyam balance 5000 o Ram balance 5000  synchronized void deposit(..) o Ram balance 1000 o Ram balance 2000 o Ram balance 3000 o Ram balance 4000 o Ram balance 5000 o Shyam balance 6000 o Shyam balance 7000 o Shyam balance 8000 o Shyam balance 9000 o Shyam balance 10000 www.SunilOS.com 32
  • 33. www.SunilOS.com 33 Synchronization types Method public synchronized void deposit(String message, int amount) {….} Block public void deposit(String message, int amount) { synchronized (this){ int bal = getBalance() + amount; setBalance(bal); } System.out.println(message + " Now Bal is " + bal); }
  • 34. www.SunilOS.com 34 Monitors / Locks  It is a lock or token of an object  The thread has lock, can access object  Other threads will be in waiting queue  One object has two monitors or tokens o One for instance methods, called Object Monitor o Second for static methods, called Class Monitor
  • 35. www.SunilOS.com 35 Monitor Keys Static Sync Methods x z y Instance Sync Methods a c b Thread1 Thread2 x() Class Monitor y() a() Object Monitor b() CLASS
  • 36. Disclaimer This is an educational presentation to enhance the skill of computer science students. This presentation is available for free to computer science students. Some internet images from different URLs are used in this presentation to simplify technical examples and correlate examples with the real world. We are grateful to owners of these URLs and pictures. www.SunilOS.com 36

Editor's Notes

  1. www.sunilos.com
  2. Cell- 92292 60504
  3. Cell- 92292 60504
  4. Cell- 92292 60504
  5. Cell- 92292 60504
  6. Cell- 92292 60504
  7. Cell- 92292 60504
  8. Cell- 92292 60504
  9. Cell- 92292 60504
  10. Cell- 92292 60504
  11. Cell- 92292 60504
  12. Cell- 92292 60504
  13. Cell- 92292 60504
  14. Cell- 92292 60504
  15. Cell- 92292 60504
  16. Cell- 92292 60504
  17. Cell- 92292 60504
  18. Cell- 92292 60504
  19. Cell- 92292 60504
  20. Cell- 92292 60504
  21. Cell- 92292 60504