Project Minimum submission Threads Deadline extended to tonight - - PDF document

project
SMART_READER_LITE
LIVE PREVIEW

Project Minimum submission Threads Deadline extended to tonight - - PDF document

Project Minimum submission Threads Deadline extended to tonight at midnight Early submitters 10 point bonus Synchronization TSP Still due on Tuesday! Before we begin Plan for today Thread synchronization


slide-1
SLIDE 1

Threads

Synchronization

Project

  • Minimum submission

– Deadline extended to tonight at midnight – Early submitters – 10 point bonus

  • TSP

– Still due on Tuesday!

Before we begin

  • Any questions?

Plan for today

  • Thread synchronization
  • Questions with threads?

Thread

  • A thread is a single sequential flow of

control within a program

  • Sometimes called:

– Execution context – Lightweight process

Thread

  • A thread is not a program

– It cannot run on its own – Runs within a program

slide-2
SLIDE 2

Thread

  • Multiple threads can be running at the same

time within a single program

Thread States

Running Ready Waiting

The start() method places a thread in the ready state The scheduler selects a thread and places it in the running state A thread that is waiting for I/O, was suspended, is sleeping, blocked, or otherwise is unable to do any more work is placed in the waiting state

join()

  • join () is NOT a static method
  • Calling join() on a thread will make the

caller wait until the thread is done

join()

public class WorkerThread extends Thread { private int result = 0; public void run() { // Perform a complicated time consuming calculation // and store the answer in the variable result } public static void main(String args[]) { WorkerThread t = new WorkerThread(); t.start(); try { t.join(); } catch ( InterruptedException ex ) {} System.out.println( result ); } }

Synchronization: Motivation

public class ConcAcess extends Thread { private static int common = 0; public void run() { int local = 0; local = common; local = local + 1; common = local; } public static void main( String args[] ) { Thread myThread = new Thread[3]; for ( int i = 0; i < 3; i++ ) { myThread[ i ] = new ConcAccess(); myThread[ i ].start() } }

Synchronization: Motivation

  • One possible solution
slide-3
SLIDE 3

Synchronization: Motivation

  • Another possible scenerio

Critical section

  • Critical section – block of code that must be

executed by at most 1 thread at a time.

  • The updating of common in the last

example (run method) would be considered a critical section.

Java locks

  • Every Java Object contains a lock that can

be held by at most one thread at any given time

  • Allows for mutually exclusive access to a

sequence of code

Synchronization: Motivation

public class ConcAcess extends Thread { private static int common = 0; public void run() { int local = 0; local = common; local = local + 1; common = local; } public static void main( String args[] ) { Thread myThread = new Thread[3]; for ( int i = 0; i < 3; i++ ) { myThread[ i ] = new ConcAccess(); myThread[ i ].start() } }

Obtain lock here Hold lock Release lock

synchronized

  • A thread can obtain the lock on a Java
  • bject by using the synchronized

statement.

Example with Java locks

public class ConcAccess extends Thread { private static int common = 0; private static final Integer lock = new Integer (0); public void run() { int local = 0; synchronized (lock) { local = common; local = local + 1; common = local; } } public static void main( String args[] ) { Thread myThread = new Thread[3]; for ( int i = 0; i < 3; i++ ) { myThread[ i ] = new ConcAccess(); myThread[ i ].start() } }

slide-4
SLIDE 4

Bad Example with Java locks

public class ConcAcess extends Thread { private static int common = 0; private Integer lock; public ConcAcess () { lock = new Integer(0); } public void run() { int local = 0; synchronized (lock) { local = common; local = local + 1; common = local; } } public static void main( String args[] ) { Thread myThread = new Thread[3]; for ( int i = 0; i < 3; i++ ) { myThread[ i ] = new ConcAccess(); myThread[ i ].start() } }

Threads and Locks

  • Questions?

Java locks with this

  • An object can choose to grab a lock to itself

when running one of its methods

public someMethod() { synchronized( this ) { … } }

Synchronized methods

  • Declaring a method to be synchronized is a

shorthand for grabbing the lock on this.

  • Is the same as

public synchronized someMethod() { … } public someMethod() { synchronized( this ) { … } }

Synchronized Static Methods

  • Java also provides synchronized static methods.
  • Before a synchronized static method is executed,

the calling thread must first obtain the class lock.

  • Since there is only one class lock, at most one

thread can hold the lock for the class (object locks can be held by different threads locking on different instances of the class).

Synchronization and Java Collections

  • From HashSet javadoc:

– Note that this implementation is not synchronized.

  • If multiple threads access a set concurrently, and at

least one of the threads modifies the set, it must be synchronized externally.

– Meaning

  • Different threads can manipulate the collection

concurrently.

slide-5
SLIDE 5

Wrapper Implementations

  • Wrapper implementations add some

functionality on top of what a collection

  • ffer

– Synchronization – Unmodifiable

  • Wrappers simply delegate all of their real

work to a specified collection

synchronization wrappers

  • The synchronization wrappers, will make the

Collection thread safe.

  • It is imperative that the user manually synchronize
  • n the returned set when iterating over it:

Set s = Collections.synchronizedSet(new HashSet()); ... synchronized(s) { Iterator i = s.iterator(); // Must be in the block while (i.hasNext()) foo(i.next()); }

synchronization wrappers

public static Collection synchronizedCollection(Collection c); public static Set synchronizedSet(Set s); public static List synchronizedList(List list); public static Map synchronizedMap(Map m); public static SortedSet synchronizedSortedSet(SortedSet s); public static SortedMap synchronizedSortedMap(SortedMap m);

Synchronization

  • Questions?
  • Let’s go through some code.