Java Concurrency Andrew V. Jones andrewj@doc.ic.ac.uk Department of - - PowerPoint PPT Presentation
Java Concurrency Andrew V. Jones andrewj@doc.ic.ac.uk Department of - - PowerPoint PPT Presentation
Java Concurrency Andrew V. Jones andrewj@doc.ic.ac.uk Department of Computing Imperial College London Feburary, 2010 Introducing Threads Threads are a way of running separate pieces of code Most of the code youve written so far will have,
Introducing Threads
Threads are a way of running separate pieces of code Most of the code you’ve written so far will have, implicitly, used a single thread Their main aim is to attempt to do more than one thing at
- nce
On a single-core machine, one thread can continue while another is “blocked” (waiting) On a dual-core (or processor) machine, they allow for parallelism of code
Andrew V. Jones Java Concurrency
Your First Thread
extends SuperClass, Thread public class ThreadExample extends SuperClass , Thread Why can’t we do this?
Andrew V. Jones Java Concurrency
Your First Thread
Two ways of creating a thread. . . extends Thread public class ThreadExample extends Thread implements Runnable public class RunnableExample implements Runnable
Andrew V. Jones Java Concurrency
Basic Requirements
To start using threads we need to implement the following method: public void run (); Lets look at some code. . .
Andrew V. Jones Java Concurrency
Scheduling Threads
Threads can be in a variety of different “states”: Ready Sleeping Running Blocked Selected Swapped .sleep(i) Java IO Request Request Fulfilled notifyAll() .start() Also: Waiting and Dead
Andrew V. Jones Java Concurrency
Scheduling Threads
Thread 1 Thread 2 Running I/O Waiting What’s the most increase we can get with two threads?
Andrew V. Jones Java Concurrency
Useful Threading Methods
Causing a Thread to sleep static void sleep(long millis ); Causing a Thread to wait indefinitely public final void wait (); Causing a Thread to wake public final void notify (); public final void notifyAll ();
Andrew V. Jones Java Concurrency
Useful Threading Methods
Preemptively move a Thread from “running” to “ready” static void yield (); We can also cause the main program to wait until a Thread
- finishes. . .
Waiting for a Thread public final void join ();
Andrew V. Jones Java Concurrency
Using notifyAll
“Producer” // produce data cond = true; notifyAll (); // continue “Consumer” if( !cond ) { wait (); } // consume data What’s wrong here?
Andrew V. Jones Java Concurrency
Using notifyAll
“Producer” // produce data cond = true; notifyAll (); // continue “Consumer” while( !cond ) { // wait for condition sleep (100); // or wait (); } // consume data
Andrew V. Jones Java Concurrency
Locking via synchronized
The synchronized keyword allows for “mutual exclusion”
- ver sections of code.
In Java, every object has an associated lock with it. Only a single object can have a lock on a piece of code at one time. The lock is released once the block of synchronized code ends. Locking with synchronized synchronized ( objectName ) { // code here }
Andrew V. Jones Java Concurrency
Locking via synchronized
Methods can be declared as synchronized: public synchronized void methodName () { ... } This is really syntactic sugar for the below: public void methodName () { synchronized ( this ) { ... } } If the running Thread is unable to obtain a lock on the synchronized object, it enters the “wait” state. The object will wait, indefinitely, to obtain the lock.
Andrew V. Jones Java Concurrency
Deadlock
Thread 1 synchronized ( A ) { synchronized ( B ) { ... } } Thread 2 synchronized ( B ) { synchronized ( A ) { ... } } What happens here?
Andrew V. Jones Java Concurrency
Race Conditions
Thread 1 Integer temp = someField; temp = temp * 3; someField = temp; Thread 2 Integer temp = someField; temp = temp + 1; someField = temp; What should the value of someField be at the end of the execution?
Andrew V. Jones Java Concurrency
Race Conditions
We can easily fix this ... Thread 1 synchronized ( someField ) { Integer temp = someField; temp = temp * 3; someField = temp; } And the same for Thread 2.
Andrew V. Jones Java Concurrency
Examples
Implementing a multi-threaded server
Andrew V. Jones Java Concurrency
Questions
Questions?
Andrew V. Jones Java Concurrency