Multithreaded Programs in Java
Tasks and Threads
• A task is an abstraction of a series of steps
– Might be done in a separate thread
– Java libraries use the Runnable interface
– work done by method run()
• Thread: a Java class for a thread
– work done by method run()
• How to associate a task with a thread?
• How to start a thread?
Creating a Task and Thread
• Warning: old way(s), new ways
• First, if you have a thread object, you can call
start() on that object
– Makes it available to be run
– When it’s time to run it, Thread’s run() is called
• So, create a thread using “old” (not good) way
– Write class that extends Thread, e.g. MyThread
– Define your own run()
– Create a MyThread object and call start() on it
• We won’t do this! Not good design
Runnables and Thread
• Use the “task abstraction” and create a class
that implements Runnable interface
– Define the run() method to do the work you want
• Now, two ways to make your task run in a
separate thread
– Create a Thread object and pass a Runnable to the
constructor
– As before, call start() on the Thread object
Do we need a Thread “manager”?
• If your code is responsible for creating a bunch
of tasks, linking them with Threads, and
starting them all, then you have things to
worry about:
– What if you start too many threads? Can you
manage the number of running threads?
– Can you shutdown all the threads?
– If one fails, can you restart it?
Executors
• An Executor is an object that manages running
tasks
– Submit a Runnable to be run with Executor’s
execute() method
– So, instead of creating a Thread for your Runnable
and calling start() on that, do this:
• Get an Executor object, say called exec
• Create a Runnable, say called myTask
• Submit for running: exec.execute(myTask)
How to Get an Executor
• Use static methods in Executors library.
• Fixed “thread pool”: at most N threads running at
one time
Executor exec =
Executors.newFixedThreadPool(MAX_THREADS);
• Unlimited number of threads
Executor exec =
Executors.newCachedThreadPool();
Summary So Far
• Create a class that implements a Runnable to
be your “worker”
• Create Runnable objects
• Create an Executor
• Submit each Runnable to the Executor which
starts it up in a separate thread
cs4240-multithreading.ppt presentation on multi threading
Synchronization
• Understand the issue with concurrent access
to shared data?
– Data could be a counter (int) or a data structure
(e.g. a Map or List or Set)
• A critical section: a block of code that can
only be safely executed by one thread at a
time
• A lock: an object that is “held” by one thread
at a time, then “released”
Synchronization in Java (1)
• Any object can serve as a lock
– Separate object: Object myLock = new Object();
– Current instance: the this object
• Enclose lines of code in a synchronized block
synchronized(myLock) {
// code here
}
• More than one thread could try to execute this code,
but one acquires the lock and the others “block” or
wait until the first thread releases the lock
Synchronized Methods
• Common situation: all the code in a method is a
critical section
– I.e. only one thread at a time should execute that
method
– E.g. a getter or setter or mutator, or something that
changes shared state info (e.g. a Map of important
data)
• Java makes it easy: add synchronized keyword to
method signature. E.g.
public synchronized void update(…) {
Summary So Far
• Concurrent access to shared data
– Can lead to serious, hard-to-find problems
– E.g. race conditions
• The concept of a lock
• Synchronized blocks of code or methods
– One thread at a time
– While first thread is executing it, others block
cs4240-multithreading.ppt presentation on multi threading
More Advanced Synchronization
• A semaphore object
– Allows simultaneous access by N threads
– If N==1, then this is known as a mutex (mutual exclusion)
– Java has a class Semaphore
• Java class CountDownLatch
– Created with a count (often a number of “worker”
threads). Say object is allWorkersDone
– Another thread (a “manager”) waits for all the workers to
call countDown() on that object
– So manager blocks with: allWorkersDone.await()
Barriers
• Java class CyclicBarrier
– A rendezvous point or barrier point
– Worker threads wait at a spot until all get there
– Then all proceed
Using CountDownLatch
• Here are some common scenarios and demo
programs for them
• You’ll use the last of these for the War card-
game program!
Scenario #1
• A “manager” thread and N “worker” threads
• Manager starts workers but then must wait for them to
finish before doing follow-up work
• Solution:
– Manager creates a CountDownLatch with value N
– After workers starts, manager calls await() on that
– When each worker completes its work, it calls
countDown() on the latch
– After all N call countDown(), manager is un-blocked and
does follow-up work
• Example use: parallel divide and conquer like
mergesort
• Code example: SyncDemo0.java
Scenario #2
• A “manager” thread and N “worker” threads
• Manager starts workers but wants them to “hold”
before doing real work until it says “go”
• Solution:
– Manager creates a CountDownLatch with value 1
– After each workers start, it calls await() on that Latch
– At some point, when ready, the manager calls
countDown() on that Latch
– Now Workers free to continue with their work
• Code example: SyncDemo1.java
Scenario #3
• Work done in “rounds” where:
– All workers wait for manager to say “go”
– Each worker does its job and then waits for next round
– Manager waits for all workers to complete a round, then does some
follow-up work
– When that’s done, manager starts next round by telling workers “go”
• Solution: combine the two previous solutions
– First Latch: hold workers until manager is ready
– Second Latch: manager waits until workers finish a round
– Worker’s run() has loop to repeat
– Manager must manage Latches, recreating them at end of round
• Example use: a card game or anything that has that kind of
structure
• Code example: SyncDemo2.java
Summary of last section
• Multiple threads may need to cooperate
– Common situation: some workers and a manager
– One thread may need to wait for one or more thread
to complete
– One or more threads may need to wait to be
“released”
– Or a combination of these situations
• Threads all access a CountDownLatch
– await() used to wait for enough calls to countDown()
End
• Unused slides follow
dfafaf
work
Thr
A
work
Thr
A
Work to
do
Thr
A
await

More Related Content

PPTX
MULTITHREADING PROGRAMMING AND I/O THREAD
PDF
Multithreading Introduction and Lifecyle of thread
PPT
Parallel programming
PPTX
PPT
JAVA MULTITHREDED PROGRAMMING - LECTURES
PPT
Programming - Java-Threads-and-Synchronization.ppt
PPTX
Multi-threaded Programming in JAVA
PPTX
Lecture 23-24.pptx
MULTITHREADING PROGRAMMING AND I/O THREAD
Multithreading Introduction and Lifecyle of thread
Parallel programming
JAVA MULTITHREDED PROGRAMMING - LECTURES
Programming - Java-Threads-and-Synchronization.ppt
Multi-threaded Programming in JAVA
Lecture 23-24.pptx

Similar to cs4240-multithreading.ppt presentation on multi threading (20)

PPTX
Multithreading in java
PPTX
Multithreading in java
PPT
Md09 multithreading
PDF
Core Java Programming Language (JSE) : Chapter XII - Threads
PPTX
Chap3 multi threaded programming
PPTX
Multithreading and concurrency in android
PPTX
Threading in java - a pragmatic primer
PPTX
MSBTE Computer Engineering JPR java. multi. threading.pptx
PPT
Java Multithreading and Concurrency
PPTX
OOP with Java - Multithreading - Module-5.pptx
PDF
Java Threads: Lightweight Processes
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
PPTX
Multithreading in java
Multithreading in java
Multithreading in java
Md09 multithreading
Core Java Programming Language (JSE) : Chapter XII - Threads
Chap3 multi threaded programming
Multithreading and concurrency in android
Threading in java - a pragmatic primer
MSBTE Computer Engineering JPR java. multi. threading.pptx
Java Multithreading and Concurrency
OOP with Java - Multithreading - Module-5.pptx
Java Threads: Lightweight Processes
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
Multithreading in java
Ad

More from ShrutiPanda12 (8)

PDF
androidsimplecalculator-200214111221.pdf
PPTX
logisticregression-120102011227-phpapp01.pptx
PPTX
Stockprice_prediction.pptx for download project
PDF
Administration_Vs_Management for free download the ppt of Android applications
PPT
HOTEL-MANAGEMENT-SYSTEM-PPT.ppt
PPTX
Database management system.pptx
PPT
logisticregression.ppt
PDF
seminar100326a.pdf
androidsimplecalculator-200214111221.pdf
logisticregression-120102011227-phpapp01.pptx
Stockprice_prediction.pptx for download project
Administration_Vs_Management for free download the ppt of Android applications
HOTEL-MANAGEMENT-SYSTEM-PPT.ppt
Database management system.pptx
logisticregression.ppt
seminar100326a.pdf
Ad

Recently uploaded (20)

PDF
Insight into Romanian Wild-Grown Heracleum sphondylium: Development of a New ...
PDF
Bacterial Diversity and Evolution Bacterial Taxonomy Lecture (4)_.pdf
PPTX
CHF refers to the condition wherein heart unable to pump a sufficient amount ...
PDF
HSE and their team are going through the hazards of the issues with learning ...
PPTX
Entrepreneurship Management and Finance - Module 1 - PPT
PDF
BA-1ST(Education)-Education and Society.pdf
PPTX
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
PDF
BA-1ST(Education)-Education and Society.pdf
PDF
3-Elementary-Education-Prototype-Syllabi-Compendium.pdf
PDF
Jana-Ojana Finals 2025 - School Quiz by Pragya - UEMK Quiz Club
PDF
BP303T PHARMACEUTICALMICROBIOLOGY UNIT 1
PPTX
Environmental Sciences and Sustainability Chapter 2
PDF
BÀI GIẢNG POWER POINT TIẾNG ANH 6 - I LEARN SMART WORLD - CẢ NĂM - NĂM 2025 (...
PPTX
MALARIA - educational ppt for students..
PDF
Design and Evaluation of a Inonotus obliquus-AgNP-Maltodextrin Delivery Syste...
PDF
Global strategy and action plan on oral health 2023 - 2030.pdf
PPTX
Juvenile delinquency-Crim Research day 3x
PPTX
Power of Gratitude: Honouring our teachers
PDF
[Medicalstudyzone.com] 1. AIIMS NOV EMBER 2015 SOLVED PAPER.pdf
Insight into Romanian Wild-Grown Heracleum sphondylium: Development of a New ...
Bacterial Diversity and Evolution Bacterial Taxonomy Lecture (4)_.pdf
CHF refers to the condition wherein heart unable to pump a sufficient amount ...
HSE and their team are going through the hazards of the issues with learning ...
Entrepreneurship Management and Finance - Module 1 - PPT
BA-1ST(Education)-Education and Society.pdf
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
BA-1ST(Education)-Education and Society.pdf
3-Elementary-Education-Prototype-Syllabi-Compendium.pdf
Jana-Ojana Finals 2025 - School Quiz by Pragya - UEMK Quiz Club
BP303T PHARMACEUTICALMICROBIOLOGY UNIT 1
Environmental Sciences and Sustainability Chapter 2
BÀI GIẢNG POWER POINT TIẾNG ANH 6 - I LEARN SMART WORLD - CẢ NĂM - NĂM 2025 (...
MALARIA - educational ppt for students..
Design and Evaluation of a Inonotus obliquus-AgNP-Maltodextrin Delivery Syste...
Global strategy and action plan on oral health 2023 - 2030.pdf
Juvenile delinquency-Crim Research day 3x
Power of Gratitude: Honouring our teachers
[Medicalstudyzone.com] 1. AIIMS NOV EMBER 2015 SOLVED PAPER.pdf

cs4240-multithreading.ppt presentation on multi threading

  • 2. Tasks and Threads • A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries use the Runnable interface – work done by method run() • Thread: a Java class for a thread – work done by method run() • How to associate a task with a thread? • How to start a thread?
  • 3. Creating a Task and Thread • Warning: old way(s), new ways • First, if you have a thread object, you can call start() on that object – Makes it available to be run – When it’s time to run it, Thread’s run() is called • So, create a thread using “old” (not good) way – Write class that extends Thread, e.g. MyThread – Define your own run() – Create a MyThread object and call start() on it • We won’t do this! Not good design
  • 4. Runnables and Thread • Use the “task abstraction” and create a class that implements Runnable interface – Define the run() method to do the work you want • Now, two ways to make your task run in a separate thread – Create a Thread object and pass a Runnable to the constructor – As before, call start() on the Thread object
  • 5. Do we need a Thread “manager”? • If your code is responsible for creating a bunch of tasks, linking them with Threads, and starting them all, then you have things to worry about: – What if you start too many threads? Can you manage the number of running threads? – Can you shutdown all the threads? – If one fails, can you restart it?
  • 6. Executors • An Executor is an object that manages running tasks – Submit a Runnable to be run with Executor’s execute() method – So, instead of creating a Thread for your Runnable and calling start() on that, do this: • Get an Executor object, say called exec • Create a Runnable, say called myTask • Submit for running: exec.execute(myTask)
  • 7. How to Get an Executor • Use static methods in Executors library. • Fixed “thread pool”: at most N threads running at one time Executor exec = Executors.newFixedThreadPool(MAX_THREADS); • Unlimited number of threads Executor exec = Executors.newCachedThreadPool();
  • 8. Summary So Far • Create a class that implements a Runnable to be your “worker” • Create Runnable objects • Create an Executor • Submit each Runnable to the Executor which starts it up in a separate thread
  • 10. Synchronization • Understand the issue with concurrent access to shared data? – Data could be a counter (int) or a data structure (e.g. a Map or List or Set) • A critical section: a block of code that can only be safely executed by one thread at a time • A lock: an object that is “held” by one thread at a time, then “released”
  • 11. Synchronization in Java (1) • Any object can serve as a lock – Separate object: Object myLock = new Object(); – Current instance: the this object • Enclose lines of code in a synchronized block synchronized(myLock) { // code here } • More than one thread could try to execute this code, but one acquires the lock and the others “block” or wait until the first thread releases the lock
  • 12. Synchronized Methods • Common situation: all the code in a method is a critical section – I.e. only one thread at a time should execute that method – E.g. a getter or setter or mutator, or something that changes shared state info (e.g. a Map of important data) • Java makes it easy: add synchronized keyword to method signature. E.g. public synchronized void update(…) {
  • 13. Summary So Far • Concurrent access to shared data – Can lead to serious, hard-to-find problems – E.g. race conditions • The concept of a lock • Synchronized blocks of code or methods – One thread at a time – While first thread is executing it, others block
  • 15. More Advanced Synchronization • A semaphore object – Allows simultaneous access by N threads – If N==1, then this is known as a mutex (mutual exclusion) – Java has a class Semaphore • Java class CountDownLatch – Created with a count (often a number of “worker” threads). Say object is allWorkersDone – Another thread (a “manager”) waits for all the workers to call countDown() on that object – So manager blocks with: allWorkersDone.await()
  • 16. Barriers • Java class CyclicBarrier – A rendezvous point or barrier point – Worker threads wait at a spot until all get there – Then all proceed
  • 17. Using CountDownLatch • Here are some common scenarios and demo programs for them • You’ll use the last of these for the War card- game program!
  • 18. Scenario #1 • A “manager” thread and N “worker” threads • Manager starts workers but then must wait for them to finish before doing follow-up work • Solution: – Manager creates a CountDownLatch with value N – After workers starts, manager calls await() on that – When each worker completes its work, it calls countDown() on the latch – After all N call countDown(), manager is un-blocked and does follow-up work • Example use: parallel divide and conquer like mergesort • Code example: SyncDemo0.java
  • 19. Scenario #2 • A “manager” thread and N “worker” threads • Manager starts workers but wants them to “hold” before doing real work until it says “go” • Solution: – Manager creates a CountDownLatch with value 1 – After each workers start, it calls await() on that Latch – At some point, when ready, the manager calls countDown() on that Latch – Now Workers free to continue with their work • Code example: SyncDemo1.java
  • 20. Scenario #3 • Work done in “rounds” where: – All workers wait for manager to say “go” – Each worker does its job and then waits for next round – Manager waits for all workers to complete a round, then does some follow-up work – When that’s done, manager starts next round by telling workers “go” • Solution: combine the two previous solutions – First Latch: hold workers until manager is ready – Second Latch: manager waits until workers finish a round – Worker’s run() has loop to repeat – Manager must manage Latches, recreating them at end of round • Example use: a card game or anything that has that kind of structure • Code example: SyncDemo2.java
  • 21. Summary of last section • Multiple threads may need to cooperate – Common situation: some workers and a manager – One thread may need to wait for one or more thread to complete – One or more threads may need to wait to be “released” – Or a combination of these situations • Threads all access a CountDownLatch – await() used to wait for enough calls to countDown()