java concurrency
play

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,


  1. Java Concurrency Andrew V. Jones andrewj@doc.ic.ac.uk Department of Computing Imperial College London Feburary, 2010

  2. 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 once 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

  3. Your First Thread extends SuperClass, Thread ThreadExample public class SuperClass , Thread extends Why can’t we do this? Andrew V. Jones Java Concurrency

  4. Your First Thread Two ways of creating a thread. . . extends Thread ThreadExample public class Thread extends implements Runnable public class RunnableExample Runnable implements Andrew V. Jones Java Concurrency

  5. Basic Requirements To start using threads we need to implement the following method: void run (); public Lets look at some code. . . Andrew V. Jones Java Concurrency

  6. Scheduling Threads Threads can be in a variety of different “states”: notifyAll() .start() Ready Sleeping Swapped Request Fulfilled .sleep( i ) Selected Running Blocked Java IO Request Also: Waiting and Dead Andrew V. Jones Java Concurrency

  7. Scheduling Threads Running I/O Thread 1 Thread 2 Waiting What’s the most increase we can get with two threads? Andrew V. Jones Java Concurrency

  8. Useful Thread ing Methods Causing a Thread to sleep sleep( long millis ); static void Causing a Thread to wait indefinitely void wait (); public final Causing a Thread to wake public final void notify (); notifyAll (); public final void Andrew V. Jones Java Concurrency

  9. Useful Thread ing Methods Preemptively move a Thread from “running” to “ready” yield (); static void We can also cause the main program to wait until a Thread finishes. . . Waiting for a Thread void join (); public final Andrew V. Jones Java Concurrency

  10. Using notifyAll “Producer” // produce data cond = true ; notifyAll (); // continue What’s wrong here? “Consumer” if ( !cond ) { wait (); } // consume data Andrew V. Jones Java Concurrency

  11. 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

  12. Locking via synchronized The synchronized keyword allows for “mutual exclusion” over 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

  13. 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

  14. Deadlock Thread 1 synchronized ( A ) { synchronized ( B ) { ... } } Thread 2 synchronized ( B ) { synchronized ( A ) { ... } } What happens here? Andrew V. Jones Java Concurrency

  15. 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

  16. 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

  17. Examples Implementing a multi-threaded server Andrew V. Jones Java Concurrency

  18. Questions Questions? Andrew V. Jones Java Concurrency

Recommend


More recommend