SlideShare a Scribd company logo
Multithreading in Java
Nelson Padua-Perez
Bill Pugh
Department of Computer Science
University of Maryland, College Park
Problem
Multiple tasks for computer
Draw & display images on screen
Check keyboard & mouse input
Send & receive data on network
Read & write files to disk
Perform useful computation (editor, browser, game)
How does computer do everything at once?
Multitasking
Multiprocessing
Multitasking (Time-Sharing)
Approach
Computer does some work on a task
Computer then quickly switch to next task
Tasks managed by operating system (scheduler)
Computer seems to work on tasks concurrently
Can improve performance by reducing waiting
Multitasking Can Aid Performance
Single task
Two tasks
Multiprocessing (Multithreading)
Approach
Multiple processing units (multiprocessor)
Computer works on several tasks in parallel
Performance can be improved
4096 processor
Cray X1
32 processor
Pentium Xeon
Dual-core AMD
Athlon X2
Perform Multiple Tasks Using…
Process
Definition – executable program loaded in memory
Has own address space
Variables & data structures (in memory)
Each process may execute a different program
Communicate via operating system, files, network
May contain multiple threads
Perform Multiple Tasks Using…
Thread
Definition – sequentially executed stream of
instructions
Shares address space with other threads
Has own execution context
Program counter, call stack (local variables)
Communicate via shared access to data
Multiple threads in process execute same program
Also known as “lightweight process”
Web Server uses
threads to handle …
Multiple simultaneous
web browser requests
Motivation for Multithreading
Captures logical structure of problem
May have concurrent interacting components
Can handle each component using separate thread
Simplifies programming for problem
Example
Multiple simultaneous
web browser requests…
Handled faster by
multiple web servers
Motivation for Multithreading
Better utilize hardware resources
When a thread is delayed, compute other threads
Given extra hardware, compute threads in parallel
Reduce overall execution time
Example
Multithreading Overview
Motivation & background
Threads
Creating Java threads
Thread states
Scheduling
Synchronization
Data races
Locks
Wait / Notify
Programming with Threads
Concurrent programming
Writing programs divided into independent tasks
Tasks may be executed in parallel on multiprocessors
Multithreading
Executing program with multiple threads in parallel
Special form of multiprocessing
Creating Threads in Java
You have to specify the work you want the
thread to do
Define a class that implements the Runnable
interface
public interface Runnable {
public void run();
}
Put the work in the run method
Create an instance of the worker class and
create a thread to run it
or hand the worker instance to an executor
Thread Class
public class Thread {
public Thread(Runnable R); // Thread  R.run()
public Thread(Runnable R, String name);
public void start(); // begin thread execution
...
}
More Thread Class Methods
public class Thread {
…
public String getName();
public void interrupt();
public boolean isAlive();
public void join();
public void setDaemon(boolean on);
public void setName(String name);
public void setPriority(int level);
public static Thread currentThread();
public static void sleep(long milliseconds);
public static void yield();
}
Creating Threads in Java
Runnable interface
Create object implementing Runnable interface
Pass it to Thread object via Thread constructor
Example
public class MyT implements Runnable {
public void run() {
… // work for thread
}
}
Thread t = new Thread(new MyT()); // create thread
t.start(); // begin running thread
… // thread executing in parallel
Alternative (Not Recommended)
Directly extend Thread class
public class MyT extends Thread {
public void run() {
… // work for thread
}
}
MyT t = new MyT(); // create thread
t.start(); // begin running thread
Why not recommended?
Not a big problem for getting started
but a bad habit for industrial strength development
The methods of the worker class and the
Thread class get all tangled up
Makes it hard to migrate to Thread Pools and
other more efficient approaches
Threads – Thread States
Java thread can be in one of these states
New – thread allocated & waiting for start()
Runnable – thread can execute
Blocked – thread waiting for event (I/O, etc.)
Terminated – thread finished
Transitions between states caused by
Invoking methods in class Thread
start(), yield(), sleep()
Other (external) events
Scheduler, I/O, returning from run()…
Threads – Thread States
State diagram
runnable
new
terminated
blocked
new start
terminate
IO, sleep, join,
request lock
IO complete,
sleep expired,
join complete,
acquire lock
Threads – Scheduling
Scheduler
Determines which runnable threads to run
Can be based on thread priority
Part of OS or Java Virtual Machine (JVM)
Many computers can run multiple threads
simultaneously (or nearly so)
Java Thread Example
public class ThreadExample implements Runnable {
public void run() {
for (int i = 0; i < 3; i++)
System.out.println(i);
}
public static void main(String[] args) {
new Thread(new ThreadExample()).start();
new Thread( new ThreadExample()).start();
System.out.println("Done");
}
}
Java Thread Example – Output
Possible outputs
0,1,2,0,1,2,Done // thread 1, thread 2, main()
0,1,2,Done,0,1,2 // thread 1, main(), thread 2
Done,0,1,2,0,1,2 // main(), thread 1, thread 2
0,0,1,1,2,Done,2 // main() & threads interleaved
thread 1: println 0, println 1, println 2
main (): thread 1, thread 2, println Done
thread 2: println 0, println 1, println 2
Daemon Threads
Why doesn’t the program quit as soon as Done is
printed?
Java threads types
User
Daemon
Provide general services
Typically never terminate
Call setDaemon() before start()
Program termination
If all non-daemon threads terminate, JVM shuts down
Might not see different interleavings
The threads in that example are too short
Each started thread will probably complete
before the next thread starts
Let’s make more threads that run longer
Data Races
public class DataRace implements Runnable {
static volatile int x;
public void run() {
for (int i = 0; i < 10000; i++) {
x++;
x--;
}
}
public static void main(String[] args) throws Exception {
Thread [] threads = new Thread[100];
for (int i = 0; i < threads.length; i++)
threads[i] = new Thread(new DataRace());
for (int i = 0; i < threads.length; i++)
threads[i].start();
for (int i = 0; i < threads.length; i++)
threads[i].join();
System.out.println(x); // x not always 0!
}
}
Why volatile
We’ll spend more time on volatile later
But volatile tells the compiler:
other threads might see reads/writes of this variable
don’t change/reorder eliminate the reads and writes
An optimizing compiler should, if it sees
x++; x--;
replace it with a no-op
if x isn’t volatile
Thread Scheduling Observations
Order thread is selected is indeterminate
Depends on scheduler, timing, chance
Scheduling is not guaranteed to be fair
Some schedules/interleavings can cause
unexpected and bad behaviors
Synchronization
can be used to control thread execution order
Using Synchronization
public class DataRace implements Runnable {
static volatile int x;
static Object lock = new Object();
public void run() {
for (int i = 0; i < 10000; i++)
synchronized(lock) {
x++; x--;
}
}
public static void main(String[] args) throws Exception {
Thread [] threads = new Thread[100];
for (int i = 0; i < threads.length; i++)
threads[i] = new Thread(new DataRace());
for (int i = 0; i < threads.length; i++)
threads[i].start();
for (int i = 0; i < threads.length; i++)
threads[i].join();
System.out.println(x); // x always 0!
}
}
Ad

More Related Content

Similar to JAVA MULTITHREDED PROGRAMMING - LECTURES (20)

Multithreading
MultithreadingMultithreading
Multithreading
backdoor
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
advanced java ppt
advanced java pptadvanced java ppt
advanced java ppt
PreetiDixit22
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
Krystian Zybała
 
Slot02 concurrency1
Slot02 concurrency1Slot02 concurrency1
Slot02 concurrency1
Viên Mai
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
Sasha Kravchuk
 
Internet Programming with Java
Internet Programming with JavaInternet Programming with Java
Internet Programming with Java
kavitha muneeshwaran
 
Java
JavaJava
Java
kavitha muneeshwaran
 
9.multi-threading latest(MB).ppt .
9.multi-threading latest(MB).ppt            .9.multi-threading latest(MB).ppt            .
9.multi-threading latest(MB).ppt .
happycocoman
 
Module - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects javaModule - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects java
KaviShetty
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
18 concurrency
18   concurrency18   concurrency
18 concurrency
dhrubo kayal
 
Basic java part_ii
Basic java part_iiBasic java part_ii
Basic java part_ii
Khaled AlGhazaly
 
concurrency
concurrencyconcurrency
concurrency
Jonathan Wagoner
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
ssusere538f7
 
Lec7!JavaThreads.ppt
Lec7!JavaThreads.pptLec7!JavaThreads.ppt
Lec7!JavaThreads.ppt
HemantSharma134028
 
Lec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreadingLec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreading
kavitamittal18
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxWEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
babayaga920391
 
Multi Threading
Multi ThreadingMulti Threading
Multi Threading
Ferdin Joe John Joseph PhD
 
Multithreading
MultithreadingMultithreading
Multithreading
backdoor
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
Krystian Zybała
 
Slot02 concurrency1
Slot02 concurrency1Slot02 concurrency1
Slot02 concurrency1
Viên Mai
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
Sasha Kravchuk
 
9.multi-threading latest(MB).ppt .
9.multi-threading latest(MB).ppt            .9.multi-threading latest(MB).ppt            .
9.multi-threading latest(MB).ppt .
happycocoman
 
Module - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects javaModule - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects java
KaviShetty
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
ssusere538f7
 
Lec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreadingLec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreading
kavitamittal18
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxWEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
babayaga920391
 

Recently uploaded (20)

RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
MODULE 03 - CLOUD COMPUTING- [BIS 613D] 2022 scheme.pptx
MODULE 03 - CLOUD COMPUTING-  [BIS 613D] 2022 scheme.pptxMODULE 03 - CLOUD COMPUTING-  [BIS 613D] 2022 scheme.pptx
MODULE 03 - CLOUD COMPUTING- [BIS 613D] 2022 scheme.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxbMain cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
SunilSingh610661
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Data Structures_Linear Data Structure Stack.pptx
Data Structures_Linear Data Structure Stack.pptxData Structures_Linear Data Structure Stack.pptx
Data Structures_Linear Data Structure Stack.pptx
RushaliDeshmukh2
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
2025 Apply BTech CEC .docx
2025 Apply BTech CEC                 .docx2025 Apply BTech CEC                 .docx
2025 Apply BTech CEC .docx
tusharmanagementquot
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
How to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdfHow to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdf
jamedlimmk
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxbMain cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
Main cotrol jdbjbdcnxbjbjzjjjcjicbjxbcjcxbjcxb
SunilSingh610661
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Data Structures_Linear Data Structure Stack.pptx
Data Structures_Linear Data Structure Stack.pptxData Structures_Linear Data Structure Stack.pptx
Data Structures_Linear Data Structure Stack.pptx
RushaliDeshmukh2
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
How to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdfHow to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdf
jamedlimmk
 
Ad

JAVA MULTITHREDED PROGRAMMING - LECTURES

  • 1. Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park
  • 2. Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read & write files to disk Perform useful computation (editor, browser, game) How does computer do everything at once? Multitasking Multiprocessing
  • 3. Multitasking (Time-Sharing) Approach Computer does some work on a task Computer then quickly switch to next task Tasks managed by operating system (scheduler) Computer seems to work on tasks concurrently Can improve performance by reducing waiting
  • 4. Multitasking Can Aid Performance Single task Two tasks
  • 5. Multiprocessing (Multithreading) Approach Multiple processing units (multiprocessor) Computer works on several tasks in parallel Performance can be improved 4096 processor Cray X1 32 processor Pentium Xeon Dual-core AMD Athlon X2
  • 6. Perform Multiple Tasks Using… Process Definition – executable program loaded in memory Has own address space Variables & data structures (in memory) Each process may execute a different program Communicate via operating system, files, network May contain multiple threads
  • 7. Perform Multiple Tasks Using… Thread Definition – sequentially executed stream of instructions Shares address space with other threads Has own execution context Program counter, call stack (local variables) Communicate via shared access to data Multiple threads in process execute same program Also known as “lightweight process”
  • 8. Web Server uses threads to handle … Multiple simultaneous web browser requests Motivation for Multithreading Captures logical structure of problem May have concurrent interacting components Can handle each component using separate thread Simplifies programming for problem Example
  • 9. Multiple simultaneous web browser requests… Handled faster by multiple web servers Motivation for Multithreading Better utilize hardware resources When a thread is delayed, compute other threads Given extra hardware, compute threads in parallel Reduce overall execution time Example
  • 10. Multithreading Overview Motivation & background Threads Creating Java threads Thread states Scheduling Synchronization Data races Locks Wait / Notify
  • 11. Programming with Threads Concurrent programming Writing programs divided into independent tasks Tasks may be executed in parallel on multiprocessors Multithreading Executing program with multiple threads in parallel Special form of multiprocessing
  • 12. Creating Threads in Java You have to specify the work you want the thread to do Define a class that implements the Runnable interface public interface Runnable { public void run(); } Put the work in the run method Create an instance of the worker class and create a thread to run it or hand the worker instance to an executor
  • 13. Thread Class public class Thread { public Thread(Runnable R); // Thread  R.run() public Thread(Runnable R, String name); public void start(); // begin thread execution ... }
  • 14. More Thread Class Methods public class Thread { … public String getName(); public void interrupt(); public boolean isAlive(); public void join(); public void setDaemon(boolean on); public void setName(String name); public void setPriority(int level); public static Thread currentThread(); public static void sleep(long milliseconds); public static void yield(); }
  • 15. Creating Threads in Java Runnable interface Create object implementing Runnable interface Pass it to Thread object via Thread constructor Example public class MyT implements Runnable { public void run() { … // work for thread } } Thread t = new Thread(new MyT()); // create thread t.start(); // begin running thread … // thread executing in parallel
  • 16. Alternative (Not Recommended) Directly extend Thread class public class MyT extends Thread { public void run() { … // work for thread } } MyT t = new MyT(); // create thread t.start(); // begin running thread
  • 17. Why not recommended? Not a big problem for getting started but a bad habit for industrial strength development The methods of the worker class and the Thread class get all tangled up Makes it hard to migrate to Thread Pools and other more efficient approaches
  • 18. Threads – Thread States Java thread can be in one of these states New – thread allocated & waiting for start() Runnable – thread can execute Blocked – thread waiting for event (I/O, etc.) Terminated – thread finished Transitions between states caused by Invoking methods in class Thread start(), yield(), sleep() Other (external) events Scheduler, I/O, returning from run()…
  • 19. Threads – Thread States State diagram runnable new terminated blocked new start terminate IO, sleep, join, request lock IO complete, sleep expired, join complete, acquire lock
  • 20. Threads – Scheduling Scheduler Determines which runnable threads to run Can be based on thread priority Part of OS or Java Virtual Machine (JVM) Many computers can run multiple threads simultaneously (or nearly so)
  • 21. Java Thread Example public class ThreadExample implements Runnable { public void run() { for (int i = 0; i < 3; i++) System.out.println(i); } public static void main(String[] args) { new Thread(new ThreadExample()).start(); new Thread( new ThreadExample()).start(); System.out.println("Done"); } }
  • 22. Java Thread Example – Output Possible outputs 0,1,2,0,1,2,Done // thread 1, thread 2, main() 0,1,2,Done,0,1,2 // thread 1, main(), thread 2 Done,0,1,2,0,1,2 // main(), thread 1, thread 2 0,0,1,1,2,Done,2 // main() & threads interleaved thread 1: println 0, println 1, println 2 main (): thread 1, thread 2, println Done thread 2: println 0, println 1, println 2
  • 23. Daemon Threads Why doesn’t the program quit as soon as Done is printed? Java threads types User Daemon Provide general services Typically never terminate Call setDaemon() before start() Program termination If all non-daemon threads terminate, JVM shuts down
  • 24. Might not see different interleavings The threads in that example are too short Each started thread will probably complete before the next thread starts Let’s make more threads that run longer
  • 25. Data Races public class DataRace implements Runnable { static volatile int x; public void run() { for (int i = 0; i < 10000; i++) { x++; x--; } } public static void main(String[] args) throws Exception { Thread [] threads = new Thread[100]; for (int i = 0; i < threads.length; i++) threads[i] = new Thread(new DataRace()); for (int i = 0; i < threads.length; i++) threads[i].start(); for (int i = 0; i < threads.length; i++) threads[i].join(); System.out.println(x); // x not always 0! } }
  • 26. Why volatile We’ll spend more time on volatile later But volatile tells the compiler: other threads might see reads/writes of this variable don’t change/reorder eliminate the reads and writes An optimizing compiler should, if it sees x++; x--; replace it with a no-op if x isn’t volatile
  • 27. Thread Scheduling Observations Order thread is selected is indeterminate Depends on scheduler, timing, chance Scheduling is not guaranteed to be fair Some schedules/interleavings can cause unexpected and bad behaviors Synchronization can be used to control thread execution order
  • 28. Using Synchronization public class DataRace implements Runnable { static volatile int x; static Object lock = new Object(); public void run() { for (int i = 0; i < 10000; i++) synchronized(lock) { x++; x--; } } public static void main(String[] args) throws Exception { Thread [] threads = new Thread[100]; for (int i = 0; i < threads.length; i++) threads[i] = new Thread(new DataRace()); for (int i = 0; i < threads.length; i++) threads[i].start(); for (int i = 0; i < threads.length; i++) threads[i].join(); System.out.println(x); // x always 0! } }