Java Concurrency Andrew V. Jones andrewj@doc.ic.ac.uk Department of - - PowerPoint PPT Presentation

java concurrency
SMART_READER_LITE
LIVE PREVIEW

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,


slide-1
SLIDE 1

Java Concurrency

Andrew V. Jones andrewj@doc.ic.ac.uk

Department of Computing Imperial College London

Feburary, 2010

slide-2
SLIDE 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

  • 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

slide-3
SLIDE 3

Your First Thread

extends SuperClass, Thread public class ThreadExample extends SuperClass , Thread Why can’t we do this?

Andrew V. Jones Java Concurrency

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Using notifyAll

“Producer” // produce data cond = true; notifyAll (); // continue “Consumer” if( !cond ) { wait (); } // consume data What’s wrong here?

Andrew V. Jones Java Concurrency

slide-11
SLIDE 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

slide-12
SLIDE 12

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

slide-13
SLIDE 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

slide-14
SLIDE 14

Deadlock

Thread 1 synchronized ( A ) { synchronized ( B ) { ... } } Thread 2 synchronized ( B ) { synchronized ( A ) { ... } } What happens here?

Andrew V. Jones Java Concurrency

slide-15
SLIDE 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

slide-16
SLIDE 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

slide-17
SLIDE 17

Examples

Implementing a multi-threaded server

Andrew V. Jones Java Concurrency

slide-18
SLIDE 18

Questions

Questions?

Andrew V. Jones Java Concurrency