SlideShare une entreprise Scribd logo
1  sur  63
CS 2110:
Parallel Programming
A brief intro to:
Parallelism, Threads, and
Concurrency
Our Goals
• Appreciate the (increasing) importance of
parallel programming
• Understand fundamental concepts:
– Parallelism, threads, multi-threading, concurrency,
locks, etc.
• See some basics of this is done in Java
• See some common uses:
– Divide and conquer, e.g. mergesort
– Worker threads in Swing
Notes!
• An area of rapid change!
– 1990s: parallel computers were $$$$
– Now: 4 core machines are commodity
• Variations between languages
• Old dogs and new tricks? Not so good…
– Educators, Java books, web pages
• Evolving frameworks, models, etc.
– E.g. Java’s getting Fork/Join in Java 1.7 (summer 11)
– MAP/REDUCE
(Multi)Process vs (Multi)Thread
• Assume a computer has one CPU
• Can only execute one statement at a time
– Thus one program at a time
• Process: an operating-system level “unit of
execution”
• Multi-processing
– Op. Sys. “time-slices” between processes
– Computer appears to do more than one program
(or background process) at a time
Tasks and Threads
• Thread: “a thread of execution”
• “Smaller”, “lighter” than a process
• smallest unit of processing that can be scheduled by
an operating system
• Has its own run-time call stack, copies of the CPU’s
registers, its own program counter, etc.
• Process has its own memory address space, but
threads share one address space
• A single program can be multi-threaded
• Time-slicing done just like in multiprocessing
• Repeat: the threads share the same memory
Task
• A task is an abstraction of a series of steps
– Might be done in a separate thread
•
• In Java, there are a number of classes /
interfaces that basically correspond to this
– Example (details soon): Runnable
– work done by method run()
Java: Statements  Tasks
• Consecutive lines of code:
Foo tmp = f1;
f1 = f2;
f2 = tmp;
• A method:
swap(f1, f2);
• A “task” object:
SwapTask task1= new SwapTask(f1,f2);
task1.run();
Huh? Why a task object?
• Actions, functions vs. objects. What’s the
difference?
Huh? Why a task object?
• Actions, functions vs. objects. What’s the
difference?
• Objects:
– Are persistent. Can be stored.
– Can be created and then used later.
– Can be attached to other things. Put in Collections.
– Contain state.
• Functions:
– Called, return (not permanent)
Java Library Classes
• For task-like things:
– Runnable, Callable
– SwingWorker, RecursiveAction, etc.
• Thread class
• Managing tasks and threads
– Executor, ExecutorService
– ForkJoinPool
• In Swing
– The Event-Dispatch Thread
– SwingUtilities.invokeLater()
Java’s Nested Classes
• You can declare a class inside another
– http://download.oracle.com/javase/tutorial/java/javaOO/nested.html
• If declared static, can use just like any class
• If not static
– Can only define objects of that type from within non-static code of the
enclosing class
– Object of inner-class type can access all fields of the object that
created it. (Useful!)
– Often used for “helper” classes, e.g. a node object used in a list or
tree.
• See demo done in Eclipse: TaskDemo.java
Possible Needs for Task Objects
• Can you think of any?
Possible Needs for Task Objects
• Can you think of any?
• Storing tasks for execution later
– Re-execution
• Undo and Redo
• Threads
Undo Operations
• A task object should:
– Be able to execute and undo a function
– Therefore will need to be able to save enough
state to “go back”
• When application executes a task:
– Create a task object and make it execute
– Store that object on a undo stack
• Undo
– Get last task object stored on stack, make it undo
Calculator App Example
• We had methods to do arithmetic operations:
public void addToMemory(double inputVal) {
memory = memory + inputVal; }
• Instead:
public void addToMemory(double inputVal) {
AddTask task = new AddTask(inputVal);
task.run();
undoStack.add(task);
}
Stack, Undo Stack
• A Stack is an important ADT
– A linear sequence of data
– Can only add to the end, remove item at the end
– LIFO organization: “last in, first out”
– Operations: push(x), pop(), sometimes top()
• Stacks important for storing delayed things to
return turn
– Run-time stack (with activation records)
– An undo stack (and a separate redo stack)
Nested class for Adding
private class AddTask implements UndoableRunnable {
private double param;
public AddTask(double inputVal) {
this.param = inputVal;
}
public void run() { // memory is field in CalcApp
memory = memory + this.param;
}
public boolean undo() {
memory = memory - this.param;
return true;
}
}
Undo operation
• In the Calc app:
public boolean undo() {
boolean result = false;
int last = undoStack.size()-1;
if ( last >= 0 ) {
UndoableRunnable task = undoStack.get(last);
result = task.undo();
undoStack.remove(last);
}
return result;
}
Java Thread Classes and Methods
• Java has some “primitives” for creating and
using threads
– Most sources teach these, but in practice they’re
hard to use well
– Now, better frameworks and libraries make using
them directly less important.
• But let’s take a quick look
Java’s Thread Class
• Class Thread: it’s method run() does its business
when that thread is run
• But you never call run(). Instead, you call start()
which lets Java start it and call run()
• To use Thread class directly (not recommended
now):
– define a subclass of Thread and override run() – not
recommended!
– Create a task as a Runnable, link it with a Thread, and then
call start() on the Thread.
• The Thread will run the Runnable’s run() method.
Creating a Task and Thread
• Again, the first of the two “old” ways
• Get a thread object, then 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 inheritance
– 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
– First way:
• Create a Thread object and pass a Runnable to the constructor
• As before, call start() on the Thread object
– Second way: hand your Runnable to a “thread manager”
object
• Several options here! These are the new good ways. More soon.
Join (not the most descriptive word)
• The Thread class defines various primitive methods you
could not implement on your own
– For example: start, which calls run in a new thread
• The join() method is one such method, essential for
coordination in this kind of computation
– Caller blocks until/unless the receiver is done executing (meaning its
run returns)
– E.g. in method foo() running in “main” thread, we call:
myThread.start(); myThread.join();
– Then this code waits (“blocks”) until myThread’s run() completes
• This style of parallel programming is often called “fork/join”
– Warning: we’ll soon see a library called “fork/join” which simplifies
things. In that, you never call join()
24
Threading in Swing
• Threading matters a lot in Swing GUIs
– You know: main’s thread ends “early”
– JFrame.setvisible(true) starts the “GUI thread”
• Swing methods run in a separate thread called the
Event-Dispatching Thread (EDT)
– Why? GUIs need to be responsive quickly
– Important for good user interaction
• But: slow tasks can block the EDT
– Makes GUI seem to hang
– Doesn’t allow parallel things to happen
Thread Rules in Swing
• All operations that update GUI components
must happen in the EDT
– These components are not thread-safe (later)
– SwingUtilities.invokeLater(Runnable r) is a method
that runs a task in the EDT when appropriate
• But execute slow tasks in separate worker
threads
• To make common tasks easier, use a
SwingWorker task
SwingWorker
• A class designed to be extended to define a
task for a worker thread
– Override method doInBackground()
This is like run() – it’s what you want to do
– Override method done()
This method is for updating the GUI afterwards
• It will be run in the EDT
• For more info, see:
http://download.oracle.com/javase/tutorial/uiswing/concurrency/
• Note you can get interim results too
Code Example
• We have a fibonacci demo that runs this
method both recursively and with a loop
• Original version
– Unresponsive until it completes all its calculations
• Need to run calls to the recursive fibonacci in
a separate thread
– See Fib2.java that uses SwingWorker to define a
task
New Java ForkJoin Framework
• Designed to support a common need
– Recursive divide and conquer code
– Look for small problems, solve without parallelism
– For larger problems
• Define a task for each subproblem
• Library provides
– a Thread manager, called a ForkJoinPool
– Methods to send your subtask objects to the pool to be run,
and your call waits until their done
– The pool handles the multithreading well
• Turns out that Java’s threads are still too “heavy-
weight”
• Will be in Java 7 standard libraries, but
available in Java 6 as a downloaded .jar file
– Get jsr166y.jar from
http://gee.cs.oswego.edu/dl/concurrency-interest/index.html
– More info here
http://www.cs.washington.edu/homes/djg/teachingMaterials/grossmanSPA
Screenshots: For single- and multi-threaded Mergesort:
Threads in Eclipse Debug window, and Mac’s CPU usage display
• text
The ForkJoinPool
• The “thread manager”
– Used when calls are made to RecursiveTask’s
methods fork(), invokeAll(), etc.
– When created, knows how many processors are
available
– Pretty sophisticated
• “Steals” time from threads that have nothing to do
Overview of How To
• Create a ForkJoinPool “thread-manager” object
• Create a task object that extends RecursiveTask
– We’ll ignore use of generics with this (see docs)
– Create a task-object for entire problem and call
invoke(task) on your ForkJoinPool
• Your task class’ compute() is like Thread.run()
– It has the code to do the divide and conquer
– First, it must check if small problem – don’t use
parallelism, solve without it
– Then, divide and create >1 new task-objects. Run them:
• Either with invokeAll(task1, task2, …). Waits for all to complete.
• Or calling fork() on first, then compute() on second, then join()
Same Ideas as Thread But...
To use the ForkJoin Framework:
• A little standard set-up code (e.g., create a ForkJoinPool)
Don’t subclass Thread Do subclass RecursiveTask<V>
Don’t override run Do override compute
Don’t call start Do call invoke, invokeAll, fork
Don’t just call join Do call join which returns answer
or
Do call invokeAll on multiple tasks
Mergesort Example
• Top-level call. Create “main” task and submit
public static void mergeSortFJRecur(Comparable[] list, int
first,
int last) {
if (last - first < RECURSE_THRESHOLD) {
MergeSort.insertionSort(list, first, last);
return;
}
Comparable[] tmpList = new Comparable[list.length];
threadPool.invoke(new SortTask(list, tmpList, first, last));
}
Mergesort’s Task-Object Nested Class
static class SortTask extends RecursiveAction {
Comparable[] list;
Comparable[] tmpList;
int first, last;
public SortTask(Comparable[] a, Comparable[] tmp,
int lo, int hi) {
this.list = a; this.tmpList = tmp;
this.first = lo; this.last = hi;
}
// continued next slide
compute() Does Task Recursion
protected void compute() { // in SortTask, continued from previous slide
if (last - first < RECURSE_THRESHOLD)
MergeSort.insertionSort(list, first, last);
else {
int mid = (first + last) / 2;
// the two recursive calls are replaced by a call to
invokeAll
SortTask task1 = new SortTask(list, tmpList, first, mid);
SortTask task2 = new SortTask(list, tmpList, mid+1, last);
invokeAll(task1, task2);
MergeSort.merge(list, first, mid, last);
}
Leaving new ForkJoin framework…
• Java since 1.5 has a more general set of
classes for “task managers”
Nice to Have 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 muchto worry
about:
– What if you start too many threads? Can you
manage the number of running threads?
– Enough processors?
– 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 “task object”
– Or if ForkJoin framework, extend RecursiveTask
• Create your task objects
• Create an Executor
– Or a ForkJoinPool
• Submit each task-object to the Executor which
starts it up in a separate thread
Concurrency and Synchronization
• Concurrency:
Issues related to multiple-threads accessing
shared data
• Synchronization:
Methods to manage and control concurrent
access to shared data by multiple-threads
• Note: Our book defines concurrent
programming and concurrency to be what
more people now call parallel programming
Possible Bugs in Multithreaded Code
• Possible bug #1
i=1; x=10; x = i + x; // x could be 12 here
• Possible bug #2
if ( ! myList.contains(x) )
myList.add(x); // x could be in list twice
• Why could these cause unexpected results?
Here’s Why
• See MSD text pp. 759-762
• Multiple threads executing same lines of code
at “same” time, accessing same data values
How 1 + 10 might be 12
• Thread 1 executes:
(x is 10, i is 1)
–Get i (1) into register 1
–Get x (10) into its register 2
(other thread has CPU)
–Add registers
–Store result (11) into x
(x is now 11)
(other thread has CPU)
(other thread has CPU)
–Do next line of code
(x changes to 12 even though
no code in this thread has
touched x)
• Thread 2 executes:
(x is 10, i is 1)
(other thread has CPU)
(other thread has CPU)
–Get i (1) into its register 1
(other thread has CPU)
(other thread has CPU)
–Get x (11) into is register 2
–Add registers
–Store result (12) into x
(x is now 12)
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 race condition: Two threads will access
something. They “compete” causing a problem
• 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
Some Java Solutions
• There are some synchronized collections
• Classes like AtomicInteger
– Stores an int
– Has methods to operate on it in a thread-safe
manner
int getAndAdd(int delta) instead of i=i+1
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
• Other Java classes
• CountDownLatch, Barriers, etc.
• No more on these in CS2110 this term
Unused slides for Spring 2011
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
workwork
Thr
A
workwork
Thr
A
Work to
do
Work to
do
Thr
A
awaitawait

Contenu connexe

Tendances (20)

Multithreading
Multithreading Multithreading
Multithreading
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and Concurrency
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threading
 
Parallel architecture &programming
Parallel architecture &programmingParallel architecture &programming
Parallel architecture &programming
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Thread
ThreadThread
Thread
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Pthread
PthreadPthread
Pthread
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
Pthread Library
Pthread LibraryPthread Library
Pthread Library
 
Correct and efficient synchronization of java thread
Correct and efficient synchronization of java threadCorrect and efficient synchronization of java thread
Correct and efficient synchronization of java thread
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Multi-Threading
Multi-ThreadingMulti-Threading
Multi-Threading
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Java
JavaJava
Java
 
Thread presentation
Thread presentationThread presentation
Thread presentation
 

En vedette

Concurrent programming in iOS
Concurrent programming in iOSConcurrent programming in iOS
Concurrent programming in iOSDongxu Yao
 
Intro to parallel computing
Intro to parallel computingIntro to parallel computing
Intro to parallel computingPiyush Mittal
 
Delphi Parallel Programming Library
Delphi Parallel Programming LibraryDelphi Parallel Programming Library
Delphi Parallel Programming LibraryMario Guedes
 
SlidesA Comparison of GPU Execution Time Prediction using Machine Learning an...
SlidesA Comparison of GPU Execution Time Prediction using Machine Learning an...SlidesA Comparison of GPU Execution Time Prediction using Machine Learning an...
SlidesA Comparison of GPU Execution Time Prediction using Machine Learning an...Marcos Gonzalez
 

En vedette (7)

Concurrent programming in iOS
Concurrent programming in iOSConcurrent programming in iOS
Concurrent programming in iOS
 
Ateji PX for Java
Ateji PX for JavaAteji PX for Java
Ateji PX for Java
 
Intro to parallel computing
Intro to parallel computingIntro to parallel computing
Intro to parallel computing
 
Transactional Memory
Transactional MemoryTransactional Memory
Transactional Memory
 
Delphi Parallel Programming Library
Delphi Parallel Programming LibraryDelphi Parallel Programming Library
Delphi Parallel Programming Library
 
SlidesA Comparison of GPU Execution Time Prediction using Machine Learning an...
SlidesA Comparison of GPU Execution Time Prediction using Machine Learning an...SlidesA Comparison of GPU Execution Time Prediction using Machine Learning an...
SlidesA Comparison of GPU Execution Time Prediction using Machine Learning an...
 
An Introduction to Python Concurrency
An Introduction to Python ConcurrencyAn Introduction to Python Concurrency
An Introduction to Python Concurrency
 

Similaire à Parallel programming

cs4240-multithreading.ppt presentation on multi threading
cs4240-multithreading.ppt presentation on multi threadingcs4240-multithreading.ppt presentation on multi threading
cs4240-multithreading.ppt presentation on multi threadingShrutiPanda12
 
1. learning programming with JavaThreads.pdf
1. learning programming with JavaThreads.pdf1. learning programming with JavaThreads.pdf
1. learning programming with JavaThreads.pdfahmadkeder8
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the WildTomer Gabel
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreadingssusere538f7
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/MultitaskingSasha Kravchuk
 
Basic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsBasic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsGary Yeh
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?hawkowl
 
Modern Java Concurrency
Modern Java ConcurrencyModern Java Concurrency
Modern Java ConcurrencyBen Evans
 
Rethinking the debugger
Rethinking the debuggerRethinking the debugger
Rethinking the debuggerIulian Dragos
 
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLAsync and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLArie Leeuwesteijn
 
Java multi threading
Java multi threadingJava multi threading
Java multi threadingRaja Sekhar
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigoujaxconf
 
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharpDeivaa
 
Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)Martijn Verburg
 
C#: Understanding ConfigureAwait(false)
C#: Understanding ConfigureAwait(false)C#: Understanding ConfigureAwait(false)
C#: Understanding ConfigureAwait(false)Shuhei Eda
 

Similaire à Parallel programming (20)

cs4240-multithreading.ppt presentation on multi threading
cs4240-multithreading.ppt presentation on multi threadingcs4240-multithreading.ppt presentation on multi threading
cs4240-multithreading.ppt presentation on multi threading
 
1. learning programming with JavaThreads.pdf
1. learning programming with JavaThreads.pdf1. learning programming with JavaThreads.pdf
1. learning programming with JavaThreads.pdf
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the Wild
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
Basic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsBasic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.js
 
Java Tutorials - Concurrency
Java Tutorials - ConcurrencyJava Tutorials - Concurrency
Java Tutorials - Concurrency
 
JS Essence
JS EssenceJS Essence
JS Essence
 
java threads
java threadsjava threads
java threads
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?
 
Modern Java Concurrency
Modern Java ConcurrencyModern Java Concurrency
Modern Java Concurrency
 
Rethinking the debugger
Rethinking the debuggerRethinking the debugger
Rethinking the debugger
 
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLAsync and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NL
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigou
 
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
 
Multi Threading
Multi ThreadingMulti Threading
Multi Threading
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)
 
C#: Understanding ConfigureAwait(false)
C#: Understanding ConfigureAwait(false)C#: Understanding ConfigureAwait(false)
C#: Understanding ConfigureAwait(false)
 

Dernier

Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 

Dernier (20)

Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

Parallel programming

  • 1. CS 2110: Parallel Programming A brief intro to: Parallelism, Threads, and Concurrency
  • 2. Our Goals • Appreciate the (increasing) importance of parallel programming • Understand fundamental concepts: – Parallelism, threads, multi-threading, concurrency, locks, etc. • See some basics of this is done in Java • See some common uses: – Divide and conquer, e.g. mergesort – Worker threads in Swing
  • 3. Notes! • An area of rapid change! – 1990s: parallel computers were $$$$ – Now: 4 core machines are commodity • Variations between languages • Old dogs and new tricks? Not so good… – Educators, Java books, web pages • Evolving frameworks, models, etc. – E.g. Java’s getting Fork/Join in Java 1.7 (summer 11) – MAP/REDUCE
  • 4. (Multi)Process vs (Multi)Thread • Assume a computer has one CPU • Can only execute one statement at a time – Thus one program at a time • Process: an operating-system level “unit of execution” • Multi-processing – Op. Sys. “time-slices” between processes – Computer appears to do more than one program (or background process) at a time
  • 5. Tasks and Threads • Thread: “a thread of execution” • “Smaller”, “lighter” than a process • smallest unit of processing that can be scheduled by an operating system • Has its own run-time call stack, copies of the CPU’s registers, its own program counter, etc. • Process has its own memory address space, but threads share one address space • A single program can be multi-threaded • Time-slicing done just like in multiprocessing • Repeat: the threads share the same memory
  • 6. Task • A task is an abstraction of a series of steps – Might be done in a separate thread • • In Java, there are a number of classes / interfaces that basically correspond to this – Example (details soon): Runnable – work done by method run()
  • 7. Java: Statements  Tasks • Consecutive lines of code: Foo tmp = f1; f1 = f2; f2 = tmp; • A method: swap(f1, f2); • A “task” object: SwapTask task1= new SwapTask(f1,f2); task1.run();
  • 8. Huh? Why a task object? • Actions, functions vs. objects. What’s the difference?
  • 9. Huh? Why a task object? • Actions, functions vs. objects. What’s the difference? • Objects: – Are persistent. Can be stored. – Can be created and then used later. – Can be attached to other things. Put in Collections. – Contain state. • Functions: – Called, return (not permanent)
  • 10. Java Library Classes • For task-like things: – Runnable, Callable – SwingWorker, RecursiveAction, etc. • Thread class • Managing tasks and threads – Executor, ExecutorService – ForkJoinPool • In Swing – The Event-Dispatch Thread – SwingUtilities.invokeLater()
  • 11. Java’s Nested Classes • You can declare a class inside another – http://download.oracle.com/javase/tutorial/java/javaOO/nested.html • If declared static, can use just like any class • If not static – Can only define objects of that type from within non-static code of the enclosing class – Object of inner-class type can access all fields of the object that created it. (Useful!) – Often used for “helper” classes, e.g. a node object used in a list or tree. • See demo done in Eclipse: TaskDemo.java
  • 12. Possible Needs for Task Objects • Can you think of any?
  • 13. Possible Needs for Task Objects • Can you think of any? • Storing tasks for execution later – Re-execution • Undo and Redo • Threads
  • 14. Undo Operations • A task object should: – Be able to execute and undo a function – Therefore will need to be able to save enough state to “go back” • When application executes a task: – Create a task object and make it execute – Store that object on a undo stack • Undo – Get last task object stored on stack, make it undo
  • 15. Calculator App Example • We had methods to do arithmetic operations: public void addToMemory(double inputVal) { memory = memory + inputVal; } • Instead: public void addToMemory(double inputVal) { AddTask task = new AddTask(inputVal); task.run(); undoStack.add(task); }
  • 16. Stack, Undo Stack • A Stack is an important ADT – A linear sequence of data – Can only add to the end, remove item at the end – LIFO organization: “last in, first out” – Operations: push(x), pop(), sometimes top() • Stacks important for storing delayed things to return turn – Run-time stack (with activation records) – An undo stack (and a separate redo stack)
  • 17. Nested class for Adding private class AddTask implements UndoableRunnable { private double param; public AddTask(double inputVal) { this.param = inputVal; } public void run() { // memory is field in CalcApp memory = memory + this.param; } public boolean undo() { memory = memory - this.param; return true; } }
  • 18. Undo operation • In the Calc app: public boolean undo() { boolean result = false; int last = undoStack.size()-1; if ( last >= 0 ) { UndoableRunnable task = undoStack.get(last); result = task.undo(); undoStack.remove(last); } return result; }
  • 19.
  • 20. Java Thread Classes and Methods • Java has some “primitives” for creating and using threads – Most sources teach these, but in practice they’re hard to use well – Now, better frameworks and libraries make using them directly less important. • But let’s take a quick look
  • 21. Java’s Thread Class • Class Thread: it’s method run() does its business when that thread is run • But you never call run(). Instead, you call start() which lets Java start it and call run() • To use Thread class directly (not recommended now): – define a subclass of Thread and override run() – not recommended! – Create a task as a Runnable, link it with a Thread, and then call start() on the Thread. • The Thread will run the Runnable’s run() method.
  • 22. Creating a Task and Thread • Again, the first of the two “old” ways • Get a thread object, then 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 inheritance – 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!
  • 23. 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 – First way: • Create a Thread object and pass a Runnable to the constructor • As before, call start() on the Thread object – Second way: hand your Runnable to a “thread manager” object • Several options here! These are the new good ways. More soon.
  • 24. Join (not the most descriptive word) • The Thread class defines various primitive methods you could not implement on your own – For example: start, which calls run in a new thread • The join() method is one such method, essential for coordination in this kind of computation – Caller blocks until/unless the receiver is done executing (meaning its run returns) – E.g. in method foo() running in “main” thread, we call: myThread.start(); myThread.join(); – Then this code waits (“blocks”) until myThread’s run() completes • This style of parallel programming is often called “fork/join” – Warning: we’ll soon see a library called “fork/join” which simplifies things. In that, you never call join() 24
  • 25.
  • 26. Threading in Swing • Threading matters a lot in Swing GUIs – You know: main’s thread ends “early” – JFrame.setvisible(true) starts the “GUI thread” • Swing methods run in a separate thread called the Event-Dispatching Thread (EDT) – Why? GUIs need to be responsive quickly – Important for good user interaction • But: slow tasks can block the EDT – Makes GUI seem to hang – Doesn’t allow parallel things to happen
  • 27. Thread Rules in Swing • All operations that update GUI components must happen in the EDT – These components are not thread-safe (later) – SwingUtilities.invokeLater(Runnable r) is a method that runs a task in the EDT when appropriate • But execute slow tasks in separate worker threads • To make common tasks easier, use a SwingWorker task
  • 28. SwingWorker • A class designed to be extended to define a task for a worker thread – Override method doInBackground() This is like run() – it’s what you want to do – Override method done() This method is for updating the GUI afterwards • It will be run in the EDT • For more info, see: http://download.oracle.com/javase/tutorial/uiswing/concurrency/ • Note you can get interim results too
  • 29. Code Example • We have a fibonacci demo that runs this method both recursively and with a loop • Original version – Unresponsive until it completes all its calculations • Need to run calls to the recursive fibonacci in a separate thread – See Fib2.java that uses SwingWorker to define a task
  • 30.
  • 31. New Java ForkJoin Framework • Designed to support a common need – Recursive divide and conquer code – Look for small problems, solve without parallelism – For larger problems • Define a task for each subproblem • Library provides – a Thread manager, called a ForkJoinPool – Methods to send your subtask objects to the pool to be run, and your call waits until their done – The pool handles the multithreading well
  • 32. • Turns out that Java’s threads are still too “heavy- weight” • Will be in Java 7 standard libraries, but available in Java 6 as a downloaded .jar file – Get jsr166y.jar from http://gee.cs.oswego.edu/dl/concurrency-interest/index.html – More info here http://www.cs.washington.edu/homes/djg/teachingMaterials/grossmanSPA
  • 33. Screenshots: For single- and multi-threaded Mergesort: Threads in Eclipse Debug window, and Mac’s CPU usage display • text
  • 34. The ForkJoinPool • The “thread manager” – Used when calls are made to RecursiveTask’s methods fork(), invokeAll(), etc. – When created, knows how many processors are available – Pretty sophisticated • “Steals” time from threads that have nothing to do
  • 35. Overview of How To • Create a ForkJoinPool “thread-manager” object • Create a task object that extends RecursiveTask – We’ll ignore use of generics with this (see docs) – Create a task-object for entire problem and call invoke(task) on your ForkJoinPool • Your task class’ compute() is like Thread.run() – It has the code to do the divide and conquer – First, it must check if small problem – don’t use parallelism, solve without it – Then, divide and create >1 new task-objects. Run them: • Either with invokeAll(task1, task2, …). Waits for all to complete. • Or calling fork() on first, then compute() on second, then join()
  • 36. Same Ideas as Thread But... To use the ForkJoin Framework: • A little standard set-up code (e.g., create a ForkJoinPool) Don’t subclass Thread Do subclass RecursiveTask<V> Don’t override run Do override compute Don’t call start Do call invoke, invokeAll, fork Don’t just call join Do call join which returns answer or Do call invokeAll on multiple tasks
  • 37. Mergesort Example • Top-level call. Create “main” task and submit public static void mergeSortFJRecur(Comparable[] list, int first, int last) { if (last - first < RECURSE_THRESHOLD) { MergeSort.insertionSort(list, first, last); return; } Comparable[] tmpList = new Comparable[list.length]; threadPool.invoke(new SortTask(list, tmpList, first, last)); }
  • 38. Mergesort’s Task-Object Nested Class static class SortTask extends RecursiveAction { Comparable[] list; Comparable[] tmpList; int first, last; public SortTask(Comparable[] a, Comparable[] tmp, int lo, int hi) { this.list = a; this.tmpList = tmp; this.first = lo; this.last = hi; } // continued next slide
  • 39. compute() Does Task Recursion protected void compute() { // in SortTask, continued from previous slide if (last - first < RECURSE_THRESHOLD) MergeSort.insertionSort(list, first, last); else { int mid = (first + last) / 2; // the two recursive calls are replaced by a call to invokeAll SortTask task1 = new SortTask(list, tmpList, first, mid); SortTask task2 = new SortTask(list, tmpList, mid+1, last); invokeAll(task1, task2); MergeSort.merge(list, first, mid, last); }
  • 40. Leaving new ForkJoin framework… • Java since 1.5 has a more general set of classes for “task managers”
  • 41. Nice to Have 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 muchto worry about: – What if you start too many threads? Can you manage the number of running threads? – Enough processors? – Can you shutdown all the threads? – If one fails, can you restart it?
  • 42. 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)
  • 43. 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();
  • 44. Summary So Far • Create a class that implements a Runnable to be your “task object” – Or if ForkJoin framework, extend RecursiveTask • Create your task objects • Create an Executor – Or a ForkJoinPool • Submit each task-object to the Executor which starts it up in a separate thread
  • 45. Concurrency and Synchronization • Concurrency: Issues related to multiple-threads accessing shared data • Synchronization: Methods to manage and control concurrent access to shared data by multiple-threads • Note: Our book defines concurrent programming and concurrency to be what more people now call parallel programming
  • 46. Possible Bugs in Multithreaded Code • Possible bug #1 i=1; x=10; x = i + x; // x could be 12 here • Possible bug #2 if ( ! myList.contains(x) ) myList.add(x); // x could be in list twice • Why could these cause unexpected results?
  • 47. Here’s Why • See MSD text pp. 759-762 • Multiple threads executing same lines of code at “same” time, accessing same data values
  • 48. How 1 + 10 might be 12 • Thread 1 executes: (x is 10, i is 1) –Get i (1) into register 1 –Get x (10) into its register 2 (other thread has CPU) –Add registers –Store result (11) into x (x is now 11) (other thread has CPU) (other thread has CPU) –Do next line of code (x changes to 12 even though no code in this thread has touched x) • Thread 2 executes: (x is 10, i is 1) (other thread has CPU) (other thread has CPU) –Get i (1) into its register 1 (other thread has CPU) (other thread has CPU) –Get x (11) into is register 2 –Add registers –Store result (12) into x (x is now 12)
  • 49. 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 race condition: Two threads will access something. They “compete” causing a problem • 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”
  • 50. 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
  • 51. 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(…) {
  • 52. 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
  • 53. Some Java Solutions • There are some synchronized collections • Classes like AtomicInteger – Stores an int – Has methods to operate on it in a thread-safe manner int getAndAdd(int delta) instead of i=i+1
  • 54. 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 • Other Java classes • CountDownLatch, Barriers, etc. • No more on these in CS2110 this term
  • 55. Unused slides for Spring 2011
  • 56. Barriers • Java class CyclicBarrier – A rendezvous point or barrier point – Worker threads wait at a spot until all get there – Then all proceed
  • 57. 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!
  • 58. 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
  • 59. 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
  • 60. 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
  • 61. 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()

Notes de l'éditeur

  1. Show “top” command running in shell in OS X or Unix.