SlideShare a Scribd company logo
www.SunilOS.com 1
www.sunilos.com
www.raystec.com
Concurrency/Threads
www.SunilOS.com 2
Process
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.
Most implementations of the Java virtual
machine run as a single process.
www.SunilOS.com 3
Concurrency vs. Parallelism
CPU CPU1 CPU2
www.SunilOS.com 4
Thread
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
(application) has at least one thread.
Threads share the process's resources, including
memory and open files.
Each thread is associated with an instance of the
class java.lang.Thread.
www.SunilOS.com 5
Threads and Processes
www.SunilOS.com 6
Multitasking & Multithreading
 Multitasking refers to a computer's ability to perform multiple
jobs concurrently. Multitasking means more than one
programs are running together on a single machine.
 A thread is a single sequence of execution within a
program.
 Multithreading refers to multiple threads of control within a
single program. Each program can run multiple threads of
control within it, e.g., Web Browser.
www.SunilOS.com 7
HelloWithoutThread
public class HelloWithoutThread {
String name = null; // Keeps Thread Name
public HelloWithoutThread(String threadName) {// Constructor
name = threadName;
}
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(i + " Hello Thread " + name);
}
}
}
www.SunilOS.com 8
TestWithoutThread
public static void main(String[] args) {
HelloWithoutThread t1 = new
HelloWithoutThread(“Raina");
HelloWithoutThread t2 = new HelloWithoutThread(“Virat");
t1.run();
t2.run();
}
www.SunilOS.com 9
TestWithoutThread Output
 0 Hello Raina
 1 Hello Raina
 2 Hello Raina
 …..
 48 Hello Raina
 49 Hello Raina
 0 Hello Virat
 1 Hello Virat
 2 Hello Virat
 ……..
 48 Hello Virat
 49 Hello Virat
www.SunilOS.com 10
Thread
Raktabija
 Raktabija had a capability
to create another Raktbija
by his single drop of blood.
 Created Raktabija will be
equally powerful, and can
consume same kind of
resources and work
concurrently.
 If one Raktabija dies
another will remain active
and live.
 Likewise a Thread can
create another thread.
 Created threads will be
equally powerful.
 Threads need same kind of
resources to be executed.
 Threads work concurrently.
 If one thread dies, other
will remain active in
memory.
www.SunilOS.com 11
http://en.wikipedia.org/wiki/Raktavija
www.SunilOS.com 12
Creating Threads
 There are two ways to create a Thread object
1. Inherit Thread class.
2. Implement the Runnable.
 In both cases the run() method should be
implemented
www.SunilOS.com 13
HelloWithThread
public class HelloWithThread extends Thread {
private String name = null;
public HelloWithThread(String threadName) {
name = threadName;
}
public void run() {
for (int i = 0; i < 500; i++) {
System.out.println(i + " Hello Thread " + name);
}
}
}
www.SunilOS.com 14
TestWithThread
public static void main(String[] args) {
HelloWithThread t1 = new HelloWithThread(“Raina");
HelloWithThread t2 = new HelloWithThread(“Virat");
t1.start();
t2.start();
for (int i = 0; i < 500; i++) {
System.out.println(" Main Thread ");
}
}
www.SunilOS.com 15
TestWithThread Output
 0 Hello Thread Raina
 1 Hello Thread Raina
 2 Hello Thread Raina
 0 Hello Thread Virat
 1 Hello Thread Virat
 1 Hello Thread main
 2 Hello Thread main
 3 Hello Thread main
 3 Hello Thread Raina
 4 Hello Thread Raina
 2 Hello Thread Virat
 3 Hello Thread Virat
 4 Hello Thread main
 ……..
 48 Hello Thread Raina
 49 Hello Thread Raina
 48 Hello Thread Virat
www.SunilOS.com 16
Scheduling Threads
I/O operation completes
start()
Currently executed
thread
Ready queue
•Waiting for I/O operation to be completed
•Waiting to be notified
•Sleeping
•Waiting to enter a synchronized section
Newly created
threads
What happens when
a program with a
ServerSocket calls
accept()?
www.SunilOS.com 17
Thread State Diagram
Alive
New Thread Dead Thread
Running
Runnable
new ThreadExample();
run() method returns
while (…) { … }
Blocked
Object.wait()
Thread.sleep()
blocking IO call
waiting on a monitor
thread.start();
www.SunilOS.com 18
Thread Lifecycle
Born
Blocked
Runnable
Dead
stop()
start()
stop()
Active
block on I/O
I/O available
JVM
sleep(500)
wake up
suspend()
resume()
wait
notify
www.SunilOS.com 19
Child of other Class -Runnable Interface
If a class is child of another class and you want to make it
Thread then use Runnable interface since Java does not
support multiple inheritance.
public class HelloThread extends Hello implements Runnable {
public void run() {
System.out.println("Hello from Thread!");
}
public static void main(String args[]) {
HelloThread runThread = new HelloThread ()
Thread th =new Thread(runThread);
th.start();
}
}
www.SunilOS.com 20
Thread Methods
void start()
o Creates a new thread and makes it runnable.
o This method can be called only once.
void run()
o The new thread begins its life inside this method.
void stop() (deprecated)
o The thread is being terminated.
www.SunilOS.com 21
Thread Methods
yield()
o Pauses the currently executing thread object and allows
other thread of same priority to be executed.
sleep(int m)/sleep(int m,int n)  
o The thread is slept for m milliseconds and n
nanoseconds.
www.SunilOS.com 22
Multiple Threads in an Application
Each thread has its private run-time stack.
If two threads execute the same method, each will
have its own copy of the local variables the
methods uses.
However, all threads see the same dynamic
memory (heap).
Two different threads can act on the same object
and same static fields concurrently.
www.SunilOS.com 23
Green Thread
 Native Thread model:
o the threads are scheduled by the OS
o maps a single Java thread to an OS thread
 Green Thread model:
o the threads are scheduled by the Virtual Machine.
o threads are invisible to the OS.
o Green threads emulate multithreaded environments without relying
on any native OS capabilities.
o They run code in user space that manages and schedules threads
o Green threads enables Java to work in environments that do not
have native thread support.
o OS doesn't know anything about the threads used in the VM. It's up
to the VM to handle all the details.
www.SunilOS.com 24
Scheduling
Thread-scheduling is the mechanism used
to determine how runnable threads are
allocated CPU time.
A thread-scheduling mechanism is either
preemptive or non-preemptive .
www.SunilOS.com 25
Preemptive Scheduling
Preemptive scheduling – the thread scheduler
preempts (pauses) a running thread and executes
other thread as per schedule.
Non-preemptive scheduling – the scheduler never
interrupts a running thread. Other thread will get
chance only if current thread dies.
The non-preemptive scheduler relies on the
running thread to yield control of the CPU so that
other threads may execute.
www.SunilOS.com 26
Starvation and Livelock
A non-preemptive scheduler may cause
starvation.
Runnable threads which are in ready queue
may have to wait to be executed for a very
log time or forever.
Sometimes, starvation is also called a
livelock.
www.SunilOS.com 27
Time-sliced Scheduling
 Time-sliced scheduling – the scheduler allocates a period of
time that each thread can use the CPU. When that amount
of time has elapsed, the scheduler preempts the thread and
switches to other thread.
 Non time-sliced scheduler – the scheduler does not use
elapsed time to determine when to preempt a thread. It
uses other criteria such as priority or I/O status.
www.SunilOS.com 28
Java Scheduling
Java-scheduler is preemptive and based on
priority of threads.
It uses fixed-priority scheduling.
Threads are scheduled according to their
priority with respect to other threads in the
ready queue.
www.SunilOS.com 29
Java Scheduling
The highest priority runnable thread is
always selected for execution above lower
priority threads.
When multiple threads have equally high
priority then only one of them will be
guaranteed to be executed.
Java threads are guaranteed to be
preemptive-but not time sliced.
www.SunilOS.com 30
Thread Priority
Every thread has a priority.
When a thread is created, it inherits the
priority of the thread that created it.
The priority values range from 1 to 10, in
increasing priority. That means 1 is lowest
and 10 is highest.
www.SunilOS.com 31
Thread Priority (cont.)
The priority can be adjusted subsequently using
the setPriority(5) method.
The priority of a thread may be obtained using
getPriority().
 Priority constants are defined in Thread class:
o MIN_PRIORITY=1
o MAX_PRIORITY=10
o NORM_PRIORITY=5
Thread th = new Thread(“Raina”);
o th.setPriority(4);
www.SunilOS.com 32
Some Notes
Thread implementation in Java is actually
based on operating system support.
Some Windows operating systems support
only 7 priority levels, so different levels in
Java may actually be mapped to the same
operating system level.
www.SunilOS.com 33
Daemon Threads
Daemon threads are “background” threads, that
provide services to other threads, e.g., the garbage
collection thread.
The Java VM will not exit if non-Daemon threads
are executing.
The Java VM will exit if only Daemon threads are
executing.
Daemon threads will die when the Java VM exits.
t.setDaemon(true)
www.SunilOS.com 34
ThreadGroup
The ThreadGroup class is used to create
groups of similar threads.
www.SunilOS.com 35
ThreadInfo
public class ThreadInfo {
public static void main(String[] args) {
Thread t = new Thread("My Thread");
log(" ",t);
}
public static void log(String indent, Thread t) {
System.out.println(indent + "THREAD Name :" + t.getName());
System.out.println(indent + "ID :" + t.getId());
System.out.println(indent + "Priority :" + t.getPriority());
System.out.println(indent + "State :" + t.getState());
System.out.println(indent + "Thread Group :"
+ t.getThreadGroup().getName());
System.out.println(indent + "Is Alive :" + t.isAlive());
System.out.println(indent + "Is Daemon :" + t.isDaemon() + "n");
}
}
www.SunilOS.com 36
ThreadInfo (Cont..)
public static void info(String indent, ThreadGroup tg) {
int threadCount = tg.activeCount(); //Get Active Thread Count
Thread[] threads = new Thread[threadCount];
tg.enumerate(threads); //Get Active Threads
System.out.println(indent + "THREAD GROUP NAME: " + tg.getName());
System.out.println(indent + "Is Daemon : " + tg.isDaemon());
//Log Threads Details
for (int i = 0; i < threads.length; i++) {
log( indent + "t" , threads[i]);
}
int groupCount = tg.activeGroupCount();//Get Active ThreadGroup Count
ThreadGroup[] groups = new ThreadGroup[groupCount];
tg.enumerate(groups); //Get ThreadGroups
for (int i = 0; i < groups.length; i++) {
info(indent + "t", groups[i]);
}
}
www.SunilOS.com 37
ThreadInfo (Cont..)
public static void main(String[] args) throws Exception{
Thread t = Thread.currentThread(); //Get Currect Thread
ThreadGroup tg = t.getThreadGroup();
while(tg.getParent() !=null){ //Get Root ThreadGroup
tg=tg.getParent() ;
}
HelloWithThread t1 = new HelloWithThread("TRaina");
t1.setPriority(1);
HelloWithThread t2 = new HelloWithThread("Yuvraj");
t2.setPriority(1);
t1.start();
t2.start();
info("", tg);
}
www.SunilOS.com 38
Concurrency
An object in a program can be changed by
more than one threads concurrently.
Q: Is the order of changes that were
preformed on the object important?
Race-Condition
www.SunilOS.com 39
www.SunilOS.com 40
Race Condition
When two threads are simultaneously
modifying a single object, is called race-
condition.
Both threads “race” to store their values in a
single object.
The outcome of a program is affected by the
order in which the program's threads are
allocated CPU time.
www.SunilOS.com 41
Deposit in an Account
Account
User1
User2
Balance = 1000
Balance = 2000
getBalance():1000
setBalance(2000)
setBalance(2000)
getBalance():1000
www.SunilOS.com 42
Synchronized access
AccountUser1
User2
Balance = 1000
Balance = 3000
getBalance():1000
setBalance(2000)
setBalance(3000)
getBalance():2000
Deposit in an Account
www.SunilOS.com 43
Instance vs static attributes
instance
static
instance
static
Attributes Methods
Person
-name : String
-address: String
$ AVR_AGE : int = 60
+getName()
+getAddress()
$ getAveAge()
+getName()
+getAddress()
$ getAveAge()
AVR_AGE = 60
Person p1, p2
p1 = new Person()
p2 = new Person()
Name = A
Address = Add1
Name = B
Address = Add2
p1
p2p1.getName ()
P2.getAvrAge()
Person.getAvrAge()
Class
1011
1010
1010
1011
2B
2B
www.SunilOS.com 44
Account
public class Account {
private int balance = 0;
public void deposit(String message, int amount) {
int bal = getBalance() ;
setBalance(bal+ amount);
System.out.println(message + " Now Balance is " +
getBalance());
}
public int getBalance() {
try {
Thread.sleep(200); // Simulate Database Operation
} catch (InterruptedException e) {}
return balance;
}
www.SunilOS.com 45
Account
public void setBalance(int balance) {
try {
Thread.sleep(200); // Simulate Database Operation
} catch (InterruptedException e) {}
this.balance = balance;
}
}
www.SunilOS.com 46
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 47
RacingCondThread
public static void main(String[] args) {
RacingCondThread t1 = new RacingCondThread(“Raina");
RacingCondThread t2 = new RacingCondThread(“Virat");
t1.start();
t2.start();
}
www.SunilOS.com 48
Account - synchronized
public class Account {
private int balance = 0;
public synchronized void deposit(String message, int
amount) {
int bal =getBalance() + amount;
setBalance(getBalance() + amount);
System.out.println(message + " Now Bal is " + bal);
}
//…
}
www.SunilOS.com 49
Monitor Keys
Static Sync Methods
x
zy
Instance Sync Methods
a
cb
Thread1
Thread2
x() Class
Monitor
y()
a() Object
Monitor
b()
CLASS
www.SunilOS.com 50
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 51
Monitors
Each object has a “monitor” that is a token used to
determine which application thread has control of a
particular object.
Access to the object monitor is queued.
There are two monitors:
o One for synchronized instance methods called Object
Monitor.
o Other for synchronized static methods called Class
Monitor.
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 52
Thank You!
www.SunilOS.com 53
www.SunilOS.com

More Related Content

PPT
Exception Handling
PPT
Java Input Output and File Handling
PPT
Collections Framework
PPT
JDBC
PPT
JAVA OOP
PPT
JAVA Variables and Operators
PPT
Jsp/Servlet
PPT
Java 8 - CJ
Exception Handling
Java Input Output and File Handling
Collections Framework
JDBC
JAVA OOP
JAVA Variables and Operators
Jsp/Servlet
Java 8 - CJ

What's hot (20)

PPT
OOP V3.1
PPT
Java IO Streams V4
PPT
Java Basics V3
PPT
Threads V4
PPT
Hibernate
PPT
Collection v3
PPT
JavaScript
PDF
Introduction to kotlin coroutines
PPT
Log4 J
PPSX
Exception Handling
PPT
Resource Bundle
PPS
String and string buffer
PPTX
20.3 Java encapsulation
PDF
Java 8 features
PPT
JUnit 4
PDF
Java Thread Synchronization
PPTX
Polymorphism in java
PPT
Java Networking
PPTX
Type casting in java
PPTX
Java 8 presentation
OOP V3.1
Java IO Streams V4
Java Basics V3
Threads V4
Hibernate
Collection v3
JavaScript
Introduction to kotlin coroutines
Log4 J
Exception Handling
Resource Bundle
String and string buffer
20.3 Java encapsulation
Java 8 features
JUnit 4
Java Thread Synchronization
Polymorphism in java
Java Networking
Type casting in java
Java 8 presentation
Ad

Viewers also liked (18)

PPT
C# Variables and Operators
PPT
Java Basics
PPT
Java Swing JFC
PDF
java-thread
PPT
Rays Technologies
PPT
C# Basics
PPT
C++ oop
PPT
Java And Multithreading
PPT
C Basics
PDF
javathreads
PPT
PDF
37 Java Interview Questions
PPTX
Thread model of java
PPT
Threads in Java
PDF
Threads concept in java
PDF
Java Course 8: I/O, Files and Streams
PPTX
ODP
Multithreading In Java
C# Variables and Operators
Java Basics
Java Swing JFC
java-thread
Rays Technologies
C# Basics
C++ oop
Java And Multithreading
C Basics
javathreads
37 Java Interview Questions
Thread model of java
Threads in Java
Threads concept in java
Java Course 8: I/O, Files and Streams
Multithreading In Java
Ad

Similar to Java Threads and Concurrency (20)

PPT
Threads v3
PPTX
Java-Threads And Concurrency Presentation. 2024
PPTX
Java Threads And Concurrency Presentation. 2024
PPT
PPT
PPT
Java Multithreading
PPT
Java multithreading
PPT
Multithreading
 
PPTX
04 threads-pbl-2-slots
PPTX
04 threads-pbl-2-slots
PPTX
Object-Oriented-Prog_MultiThreading.pptx
PPT
PPT
multithreading
PPTX
Multithreading
PPTX
Threading.pptx
PPTX
Internet Programming with Java
PDF
Multithreading Introduction and Lifecyle of thread
Threads v3
Java-Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024
Java Multithreading
Java multithreading
Multithreading
 
04 threads-pbl-2-slots
04 threads-pbl-2-slots
Object-Oriented-Prog_MultiThreading.pptx
multithreading
Multithreading
Threading.pptx
Internet Programming with Java
Multithreading Introduction and Lifecyle of thread

More from Sunil OS (11)

PPT
DJango
PPT
PDBC
PPT
OOP v3
PPT
Exception Handling v3
PPTX
Machine learning ( Part 3 )
PPTX
Machine learning ( Part 2 )
PPTX
Machine learning ( Part 1 )
PPT
Python Pandas
PPT
Python part2 v1
PPT
Angular 8
PPT
Python Part 1
DJango
PDBC
OOP v3
Exception Handling v3
Machine learning ( Part 3 )
Machine learning ( Part 2 )
Machine learning ( Part 1 )
Python Pandas
Python part2 v1
Angular 8
Python Part 1

Recently uploaded (20)

DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Pharma ospi slides which help in ospi learning
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
UPPER GASTRO INTESTINAL DISORDER.docx
Revamp in MTO Odoo 18 Inventory - Odoo Slides
Week 4 Term 3 Study Techniques revisited.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
NOI Hackathon - Summer Edition - GreenThumber.pptx
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Renaissance Architecture: A Journey from Faith to Humanism
Pharma ospi slides which help in ospi learning
human mycosis Human fungal infections are called human mycosis..pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Anesthesia in Laparoscopic Surgery in India
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx

Java Threads and Concurrency

  • 2. www.SunilOS.com 2 Process 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. Most implementations of the Java virtual machine run as a single process.
  • 3. www.SunilOS.com 3 Concurrency vs. Parallelism CPU CPU1 CPU2
  • 4. www.SunilOS.com 4 Thread 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 (application) has at least one thread. Threads share the process's resources, including memory and open files. Each thread is associated with an instance of the class java.lang.Thread.
  • 6. www.SunilOS.com 6 Multitasking & Multithreading  Multitasking refers to a computer's ability to perform multiple jobs concurrently. Multitasking means more than one programs are running together on a single machine.  A thread is a single sequence of execution within a program.  Multithreading refers to multiple threads of control within a single program. Each program can run multiple threads of control within it, e.g., Web Browser.
  • 7. www.SunilOS.com 7 HelloWithoutThread public class HelloWithoutThread { String name = null; // Keeps Thread Name public HelloWithoutThread(String threadName) {// Constructor name = threadName; } public void run() { for (int i = 0; i < 50; i++) { System.out.println(i + " Hello Thread " + name); } } }
  • 8. www.SunilOS.com 8 TestWithoutThread public static void main(String[] args) { HelloWithoutThread t1 = new HelloWithoutThread(“Raina"); HelloWithoutThread t2 = new HelloWithoutThread(“Virat"); t1.run(); t2.run(); }
  • 9. www.SunilOS.com 9 TestWithoutThread Output  0 Hello Raina  1 Hello Raina  2 Hello Raina  …..  48 Hello Raina  49 Hello Raina  0 Hello Virat  1 Hello Virat  2 Hello Virat  ……..  48 Hello Virat  49 Hello Virat
  • 11. Raktabija  Raktabija had a capability to create another Raktbija by his single drop of blood.  Created Raktabija will be equally powerful, and can consume same kind of resources and work concurrently.  If one Raktabija dies another will remain active and live.  Likewise a Thread can create another thread.  Created threads will be equally powerful.  Threads need same kind of resources to be executed.  Threads work concurrently.  If one thread dies, other will remain active in memory. www.SunilOS.com 11 http://en.wikipedia.org/wiki/Raktavija
  • 12. www.SunilOS.com 12 Creating Threads  There are two ways to create a Thread object 1. Inherit Thread class. 2. Implement the Runnable.  In both cases the run() method should be implemented
  • 13. www.SunilOS.com 13 HelloWithThread public class HelloWithThread extends Thread { private String name = null; public HelloWithThread(String threadName) { name = threadName; } public void run() { for (int i = 0; i < 500; i++) { System.out.println(i + " Hello Thread " + name); } } }
  • 14. www.SunilOS.com 14 TestWithThread public static void main(String[] args) { HelloWithThread t1 = new HelloWithThread(“Raina"); HelloWithThread t2 = new HelloWithThread(“Virat"); t1.start(); t2.start(); for (int i = 0; i < 500; i++) { System.out.println(" Main Thread "); } }
  • 15. www.SunilOS.com 15 TestWithThread Output  0 Hello Thread Raina  1 Hello Thread Raina  2 Hello Thread Raina  0 Hello Thread Virat  1 Hello Thread Virat  1 Hello Thread main  2 Hello Thread main  3 Hello Thread main  3 Hello Thread Raina  4 Hello Thread Raina  2 Hello Thread Virat  3 Hello Thread Virat  4 Hello Thread main  ……..  48 Hello Thread Raina  49 Hello Thread Raina  48 Hello Thread Virat
  • 16. www.SunilOS.com 16 Scheduling Threads I/O operation completes start() Currently executed thread Ready queue •Waiting for I/O operation to be completed •Waiting to be notified •Sleeping •Waiting to enter a synchronized section Newly created threads What happens when a program with a ServerSocket calls accept()?
  • 17. www.SunilOS.com 17 Thread State Diagram Alive New Thread Dead Thread Running Runnable new ThreadExample(); run() method returns while (…) { … } Blocked Object.wait() Thread.sleep() blocking IO call waiting on a monitor thread.start();
  • 18. www.SunilOS.com 18 Thread Lifecycle Born Blocked Runnable Dead stop() start() stop() Active block on I/O I/O available JVM sleep(500) wake up suspend() resume() wait notify
  • 19. www.SunilOS.com 19 Child of other Class -Runnable Interface If a class is child of another class and you want to make it Thread then use Runnable interface since Java does not support multiple inheritance. public class HelloThread extends Hello implements Runnable { public void run() { System.out.println("Hello from Thread!"); } public static void main(String args[]) { HelloThread runThread = new HelloThread () Thread th =new Thread(runThread); th.start(); } }
  • 20. www.SunilOS.com 20 Thread Methods void start() o Creates a new thread and makes it runnable. o This method can be called only once. void run() o The new thread begins its life inside this method. void stop() (deprecated) o The thread is being terminated.
  • 21. www.SunilOS.com 21 Thread Methods yield() o Pauses the currently executing thread object and allows other thread of same priority to be executed. sleep(int m)/sleep(int m,int n)   o The thread is slept for m milliseconds and n nanoseconds.
  • 22. www.SunilOS.com 22 Multiple Threads in an Application Each thread has its private run-time stack. If two threads execute the same method, each will have its own copy of the local variables the methods uses. However, all threads see the same dynamic memory (heap). Two different threads can act on the same object and same static fields concurrently.
  • 23. www.SunilOS.com 23 Green Thread  Native Thread model: o the threads are scheduled by the OS o maps a single Java thread to an OS thread  Green Thread model: o the threads are scheduled by the Virtual Machine. o threads are invisible to the OS. o Green threads emulate multithreaded environments without relying on any native OS capabilities. o They run code in user space that manages and schedules threads o Green threads enables Java to work in environments that do not have native thread support. o OS doesn't know anything about the threads used in the VM. It's up to the VM to handle all the details.
  • 24. www.SunilOS.com 24 Scheduling Thread-scheduling is the mechanism used to determine how runnable threads are allocated CPU time. A thread-scheduling mechanism is either preemptive or non-preemptive .
  • 25. www.SunilOS.com 25 Preemptive Scheduling Preemptive scheduling – the thread scheduler preempts (pauses) a running thread and executes other thread as per schedule. Non-preemptive scheduling – the scheduler never interrupts a running thread. Other thread will get chance only if current thread dies. The non-preemptive scheduler relies on the running thread to yield control of the CPU so that other threads may execute.
  • 26. www.SunilOS.com 26 Starvation and Livelock A non-preemptive scheduler may cause starvation. Runnable threads which are in ready queue may have to wait to be executed for a very log time or forever. Sometimes, starvation is also called a livelock.
  • 27. www.SunilOS.com 27 Time-sliced Scheduling  Time-sliced scheduling – the scheduler allocates a period of time that each thread can use the CPU. When that amount of time has elapsed, the scheduler preempts the thread and switches to other thread.  Non time-sliced scheduler – the scheduler does not use elapsed time to determine when to preempt a thread. It uses other criteria such as priority or I/O status.
  • 28. www.SunilOS.com 28 Java Scheduling Java-scheduler is preemptive and based on priority of threads. It uses fixed-priority scheduling. Threads are scheduled according to their priority with respect to other threads in the ready queue.
  • 29. www.SunilOS.com 29 Java Scheduling The highest priority runnable thread is always selected for execution above lower priority threads. When multiple threads have equally high priority then only one of them will be guaranteed to be executed. Java threads are guaranteed to be preemptive-but not time sliced.
  • 30. www.SunilOS.com 30 Thread Priority Every thread has a priority. When a thread is created, it inherits the priority of the thread that created it. The priority values range from 1 to 10, in increasing priority. That means 1 is lowest and 10 is highest.
  • 31. www.SunilOS.com 31 Thread Priority (cont.) The priority can be adjusted subsequently using the setPriority(5) method. The priority of a thread may be obtained using getPriority().  Priority constants are defined in Thread class: o MIN_PRIORITY=1 o MAX_PRIORITY=10 o NORM_PRIORITY=5 Thread th = new Thread(“Raina”); o th.setPriority(4);
  • 32. www.SunilOS.com 32 Some Notes Thread implementation in Java is actually based on operating system support. Some Windows operating systems support only 7 priority levels, so different levels in Java may actually be mapped to the same operating system level.
  • 33. www.SunilOS.com 33 Daemon Threads Daemon threads are “background” threads, that provide services to other threads, e.g., the garbage collection thread. The Java VM will not exit if non-Daemon threads are executing. The Java VM will exit if only Daemon threads are executing. Daemon threads will die when the Java VM exits. t.setDaemon(true)
  • 34. www.SunilOS.com 34 ThreadGroup The ThreadGroup class is used to create groups of similar threads.
  • 35. www.SunilOS.com 35 ThreadInfo public class ThreadInfo { public static void main(String[] args) { Thread t = new Thread("My Thread"); log(" ",t); } public static void log(String indent, Thread t) { System.out.println(indent + "THREAD Name :" + t.getName()); System.out.println(indent + "ID :" + t.getId()); System.out.println(indent + "Priority :" + t.getPriority()); System.out.println(indent + "State :" + t.getState()); System.out.println(indent + "Thread Group :" + t.getThreadGroup().getName()); System.out.println(indent + "Is Alive :" + t.isAlive()); System.out.println(indent + "Is Daemon :" + t.isDaemon() + "n"); } }
  • 36. www.SunilOS.com 36 ThreadInfo (Cont..) public static void info(String indent, ThreadGroup tg) { int threadCount = tg.activeCount(); //Get Active Thread Count Thread[] threads = new Thread[threadCount]; tg.enumerate(threads); //Get Active Threads System.out.println(indent + "THREAD GROUP NAME: " + tg.getName()); System.out.println(indent + "Is Daemon : " + tg.isDaemon()); //Log Threads Details for (int i = 0; i < threads.length; i++) { log( indent + "t" , threads[i]); } int groupCount = tg.activeGroupCount();//Get Active ThreadGroup Count ThreadGroup[] groups = new ThreadGroup[groupCount]; tg.enumerate(groups); //Get ThreadGroups for (int i = 0; i < groups.length; i++) { info(indent + "t", groups[i]); } }
  • 37. www.SunilOS.com 37 ThreadInfo (Cont..) public static void main(String[] args) throws Exception{ Thread t = Thread.currentThread(); //Get Currect Thread ThreadGroup tg = t.getThreadGroup(); while(tg.getParent() !=null){ //Get Root ThreadGroup tg=tg.getParent() ; } HelloWithThread t1 = new HelloWithThread("TRaina"); t1.setPriority(1); HelloWithThread t2 = new HelloWithThread("Yuvraj"); t2.setPriority(1); t1.start(); t2.start(); info("", tg); }
  • 38. www.SunilOS.com 38 Concurrency An object in a program can be changed by more than one threads concurrently. Q: Is the order of changes that were preformed on the object important?
  • 40. www.SunilOS.com 40 Race Condition When two threads are simultaneously modifying a single object, is called race- condition. Both threads “race” to store their values in a single object. The outcome of a program is affected by the order in which the program's threads are allocated CPU time.
  • 41. www.SunilOS.com 41 Deposit in an Account Account User1 User2 Balance = 1000 Balance = 2000 getBalance():1000 setBalance(2000) setBalance(2000) getBalance():1000
  • 42. www.SunilOS.com 42 Synchronized access AccountUser1 User2 Balance = 1000 Balance = 3000 getBalance():1000 setBalance(2000) setBalance(3000) getBalance():2000 Deposit in an Account
  • 43. www.SunilOS.com 43 Instance vs static attributes instance static instance static Attributes Methods Person -name : String -address: String $ AVR_AGE : int = 60 +getName() +getAddress() $ getAveAge() +getName() +getAddress() $ getAveAge() AVR_AGE = 60 Person p1, p2 p1 = new Person() p2 = new Person() Name = A Address = Add1 Name = B Address = Add2 p1 p2p1.getName () P2.getAvrAge() Person.getAvrAge() Class 1011 1010 1010 1011 2B 2B
  • 44. www.SunilOS.com 44 Account public class Account { private int balance = 0; public void deposit(String message, int amount) { int bal = getBalance() ; setBalance(bal+ amount); System.out.println(message + " Now Balance is " + getBalance()); } public int getBalance() { try { Thread.sleep(200); // Simulate Database Operation } catch (InterruptedException e) {} return balance; }
  • 45. www.SunilOS.com 45 Account public void setBalance(int balance) { try { Thread.sleep(200); // Simulate Database Operation } catch (InterruptedException e) {} this.balance = balance; } }
  • 46. www.SunilOS.com 46 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); } }
  • 47. www.SunilOS.com 47 RacingCondThread public static void main(String[] args) { RacingCondThread t1 = new RacingCondThread(“Raina"); RacingCondThread t2 = new RacingCondThread(“Virat"); t1.start(); t2.start(); }
  • 48. www.SunilOS.com 48 Account - synchronized public class Account { private int balance = 0; public synchronized void deposit(String message, int amount) { int bal =getBalance() + amount; setBalance(getBalance() + amount); System.out.println(message + " Now Bal is " + bal); } //… }
  • 49. www.SunilOS.com 49 Monitor Keys Static Sync Methods x zy Instance Sync Methods a cb Thread1 Thread2 x() Class Monitor y() a() Object Monitor b() CLASS
  • 50. www.SunilOS.com 50 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); }
  • 51. www.SunilOS.com 51 Monitors Each object has a “monitor” that is a token used to determine which application thread has control of a particular object. Access to the object monitor is queued. There are two monitors: o One for synchronized instance methods called Object Monitor. o Other for synchronized static methods called Class Monitor.
  • 52. 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 52