Full HTML for

Basic foilset Elements of Concurrent Programming in Java

Given by Lukasz Beca at Tango Group Internal Technology Seminars on Spring 99. Foils prepared May 19 99
Outside Index Summary of Material


Motivation
Support for threads in JAVA
Safety
Liveness
State-dependent actions
Design issues
Final remarks

Table of Contents for full HTML of Elements of Concurrent Programming in Java

Denote Foils where Image Critical
Denote Foils where Image has important information
Denote Foils where HTML is sufficient

1 Elements of Concurrent Programming in Java
2 Contents
3 Motivation
4 Potential Problems
5 Concurrency Support in Java
6 Object as a Monitor
7 Thread Construction
8 Thread Construction - Example
9 Thread Construction - Example
10 Thread Creation
11 Thread Construction - Example
12 Thread Construction - Example
13 Thread Control Methods
14 Priorities and Scheduling
15 Synchronization
16 Waiting and Notification
17 Waiting and Notification
18 Safety
19 Fully Synchronized Objects
20 Synchronization - Example
21 Synchronization - Example
22 Contained Objects
23 Liveness
24 Instance Variable Analysis
25 Splitting Synchronization
26 Splitting Classes - Example
27 Splitting Classes - Example
28 Splitting Locks - Example
29 State Dependent Actions
30 Policies
31 Guarded Suspension
32 Guarded Suspension - Example
33 Balking
34 Balking - Example
35 Optimistic Control
36 Optimistic Control - Example
37 Design Issues
38 Final Remarks
39 Readings

Outside Index Summary of Material



HTML version of Basic Foils prepared May 19 99

Foil 1 Elements of Concurrent Programming in Java

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Lukasz Beca
NPAC, Syracuse University

HTML version of Basic Foils prepared May 19 99

Foil 2 Contents

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Motivation
Support for threads in JAVA
Safety
Liveness
State-dependent actions
Design issues
Final remarks

HTML version of Basic Foils prepared May 19 99

Foil 3 Motivation

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Several tasks at the same time
Availability
Asynchronous messages
Parallelism
Implementation requirements

HTML version of Basic Foils prepared May 19 99

Foil 4 Potential Problems

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Increased complexity
Safety problems
Liveness problems
Nondeterminism
Overhead
  • thread construction
  • context switching
  • synchronization

HTML version of Basic Foils prepared May 19 99

Foil 5 Concurrency Support in Java

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Class java.lang.Thread used to initiate and control new activities
Keywords
  • synchronized
  • volatile
control execution of code in objects that may participate in multiple threads
Methods in java.lang.Object - coordinate activities across threads
  • wait
  • notify
  • notifyAll

HTML version of Basic Foils prepared May 19 99

Foil 6 Object as a Monitor

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Synchronized method
Synchronized block
Unsynchronized method
Lock
Monitor Queue
Object
public synchronized void methodA(){
// any code
}
objectA.methodA();
synchronized(objectA){
// any code
}

HTML version of Basic Foils prepared May 19 99

Foil 7 Thread Construction

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Thread class
  • String identifier, useful for tracking and debugging
  • ThreadGroup class used to enforce security policies
  • setDaemon method indicates that the task should be terminated when all non-daemon threads in the program terminate
Runnable interface, used if the class must subclass some other class (the most common example being Applet)

HTML version of Basic Foils prepared May 19 99

Foil 8 Thread Construction - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
public class Printer implements Runnable {
protected String _text;
public Printer(String text) {
_text = text;
}
public void run() {
System.out.println(_text);
}
}

HTML version of Basic Foils prepared May 19 99

Foil 9 Thread Construction - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
// code here
Printer hello = new Printer("hello");
Thread helloThread = new Thread(hello);
helloThread.start();
// code here

HTML version of Basic Foils prepared May 19 99

Foil 10 Thread Creation

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
main thread
main thread continues
new thread started
start/run

HTML version of Basic Foils prepared May 19 99

Foil 11 Thread Construction - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
public class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
public void run() {
System.out.println("DONE! " + getName());
}
}

HTML version of Basic Foils prepared May 19 99

Foil 12 Thread Construction - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
public class TwoThreadsTest {
public static void main (String[] args) {
new SimpleThread("Jamaica").start();
new SimpleThread("Fiji").start();
}
}

HTML version of Basic Foils prepared May 19 99

Foil 13 Thread Control Methods

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
start calls run method as an independent activity
stop irrevocably terminates a thread
isAlive returns true if thread started but not terminated
suspend temporarily halts a thread
resume makes thread to continue if it was suspended
sleep suspends for a given time
join suspends caller till the target thread completes
interrupt causes a sleep, wait, or join to abort with InterruptedException

HTML version of Basic Foils prepared May 19 99

Foil 14 Priorities and Scheduling

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Priority based scheduling queue
Default: thread has the same priority as the thread that created it
setPriority method changes a priority
yield method relinquishes control
Algorithm:
  • two or more threads with the highest priority: pick arbitrary one
  • lower priority thread is preempted if a higher priority thread needs to be run

HTML version of Basic Foils prepared May 19 99

Foil 15 Synchronization

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Primitive operations are atomic
  • exception: assignments to long and double variables
synchronized methods
synchronized(anyObject){ anyCode()} blocks
class-level static methods and blocks synchronization
synchronized qualifier can be overridden in subclasses

HTML version of Basic Foils prepared May 19 99

Foil 16 Waiting and Notification

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Methods wait, notify and notifyAll can be invoked only when the synchronization lock is held on their targets
wait - resulting actions
  • suspend current thread
  • place thread on the wait queue
  • release lock for the target object (all other locks retained)

HTML version of Basic Foils prepared May 19 99

Foil 17 Waiting and Notification

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
notify - resulting actions
  • remove arbitrary thread T from the wait queue
  • T must re-obtain the synchronization lock for the target object
  • resume T at the point of its wait
notifyAll works in the same way, but the steps occur for all threads in the wait queue
wait with time parameter - notify is invoked automatically when the specified time passes

HTML version of Basic Foils prepared May 19 99

Foil 18 Safety

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Nothing bad ever happens
Causes of safety problems
  • read/write conflicts: access by clients to illegal transient state values
  • write/write conflicts: inconsistent assignment to variables by concurrently executing update methods
Safe objects
  • Immutable objects - avoid state changes (example: String class)
  • Fully synchronized objects - dynamically ensure exclusive access
  • Contained objects - structurally ensure exclusive access, only one thread at a time can access an object

HTML version of Basic Foils prepared May 19 99

Foil 19 Fully Synchronized Objects

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
All methods accessing object state are synchronized
Static fields and methods: use Class locks
Partial synchronization: achieved using synchronized(this) code blocks within critical sections

HTML version of Basic Foils prepared May 19 99

Foil 20 Synchronization - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
public class LinkedCell {
protected double value_; // mutable
protected LinkedCell next_; // immutable
public LinkedCell(double v, LinkedCell t) {
value_ = v;
next_ = t;
}
public synchronized double value() {
return value_;
}
public synchronized void setValue(double v) {
value_ = v;
}

HTML version of Basic Foils prepared May 19 99

Foil 21 Synchronization - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
public LinkedCell next() {
return next_;
}
public boolean includes(double x) {
synchronized(this) {
if (value_ == x)
return true;
}
if (next() == null)
return false;
else
return next().includes(x);
}

HTML version of Basic Foils prepared May 19 99

Foil 22 Contained Objects

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Inner objects are constructed during construction of outer object
Outer object never passes references to inner objects accessible to any other object
Outer object is fully synchronized or is in turn uniquely embedded within another fully synchronized object

HTML version of Basic Foils prepared May 19 99

Foil 23 Liveness

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Something finally happens
Liveness failures
  • contention: a thread fails to run even though it is in a runnable state because another thread has taken over CPU
  • dormancy: a non-runnable thread fails to become runnable
  • deadlock: two or more threads block each other
  • premature termination: a thread is killed before it should be
Instance variable analysis: allows to find methods that need not be synchronized
Splitting synchronization: partition of class into independent, non-interacting subsets

HTML version of Basic Foils prepared May 19 99

Foil 24 Instance Variable Analysis

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Unsynchronized accessors
  • variable never has illegal transient value when other methods are executed
  • variable must be atomically assignable
Unsynchronized updates
  • values of variable remain legal across all possible interleavings of all possible methods
  • no special synchronized actions need to be performed when the variable changes to particular state

HTML version of Basic Foils prepared May 19 99

Foil 25 Splitting Synchronization

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Splitting classes: more efficient use of fine-grained synchronization measures, component classes are independently synchronized
Splitting locks: separate synchronization of different subsets of functionality

HTML version of Basic Foils prepared May 19 99

Foil 26 Splitting Classes - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
public class Shape {
protected double x_ = 0.0;
protected double y_ = 0.0;
protected double width_ = 0.0;
protected double height_ = 0.0;
public synchronized double x() {
return x_;
}
public synchronized double width() {
return width_;
}
// ...
}

HTML version of Basic Foils prepared May 19 99

Foil 27 Splitting Classes - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
public class BetterShape {
protected Location loc_ = new Location(0,0);
protected Dimentions dim_ = new Dimentions(0,0);
public double x() {
return loc_.getX();
}
public double width() {
return dim_.getWidth();
}
// ...
}

HTML version of Basic Foils prepared May 19 99

Foil 28 Splitting Locks - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
public class EvenBetterShape {
protected double x_ = 0.0;
protected double width_ = 0.0;
protected Object locationLock_ = new Object();
protected Object dimensionLock_ = new Object();
public double x() {
synchronized(locationLock_) {
return x_;
}
}
public double width() {
synchronized(dimensionLock_) {
return width_;
}
}
}

HTML version of Basic Foils prepared May 19 99

Foil 29 State Dependent Actions

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Action triggering conditions:
  • external - method on the object was called
  • internal - the object is in appropriate state
State-based preconditions: object must be in proper state upon the reception of the message
State-based postconditions: object must be in proper state when the message handling is finished

HTML version of Basic Foils prepared May 19 99

Foil 30 Policies

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Inaction ignore the request
Balking return failure indication to client
Guarded suspension (guarding) suspend execution until preconditions become true
Provisional action pretend to perform an action, but do not commit until success is assured
Rollback/Recovery try to proceed but do the rollback upon failure
Retry repeatedly attempt action

HTML version of Basic Foils prepared May 19 99

Foil 31 Guarded Suspension

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
For each condition that needs to be waited on, write a guarded wait loop
Ensure that every method causing state changes that affects the conditions invokes notifyAll to wake up any threads waiting for state changes

HTML version of Basic Foils prepared May 19 99

Foil 32 Guarded Suspension - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
public class GuardedClass {
protected boolean cond_ = false;
protected synchronized void awaitCond() {
while(!cond) {
try { wait(); }
catch(InterrrupedException ex) {}
}
}
public synchronized void guardedAction() {
awaitCond();
//actions
}
public synchronized void setCond(boolen cond) {
cond_ = cond;
notifyAll();
}
}

HTML version of Basic Foils prepared May 19 99

Foil 33 Balking

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Define appropriate exception types to be thrown to indicate the failure
Check preconditions and conditionally return failure before taking any irreversible action in the method
Make public accessor methods so that clients can avoid or minimize failures

HTML version of Basic Foils prepared May 19 99

Foil 34 Balking - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
public class IncrementException extends Exception {}
public class BalkingBoundedCounter {
protected long count_ = BoundedCounter.MIN;
public synchronized void inc() throws IncrementException {
if (count_ >= BoundedCounter.MAX)
throw new IncrementException();
else
++ count_;
}
}

HTML version of Basic Foils prepared May 19 99

Foil 35 Optimistic Control

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Means of detecting failure
  • assessing logical state
  • catching exceptions
Policy for dealing with failure
  • inaction
  • throwing exceptions
  • retrying actions
Means of dealing with the consequences of actions leading to failure
  • provisional action
  • rollback/recovery

HTML version of Basic Foils prepared May 19 99

Foil 36 Optimistic Control - Example

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
public class Optimistic {
private State currentState_;
protected synchronized boolean commit(State assumed, State next) {
boolean success = (currentState_ = assumed);
if (success)
currentState_ = next;
return success;
}
}

HTML version of Basic Foils prepared May 19 99

Foil 37 Design Issues

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Concurrency control - layering of synchronization and control policies over base mechanisms
Services in threads - how and when to create and invoke thread-based services
Flow patterns - design of system composed of connected stages with assigned producer and consumer roles
Transactions - operations performed without interference from other threads
Notifications - information about changes in state and events
Scheduling - implementing proprietary scheduling schemes

HTML version of Basic Foils prepared May 19 99

Foil 38 Final Remarks

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Design thread structure and interactions carefully
Do not use threads if it is not necessary
Never make assumptions about relative rate of progress among threads
Assume that execution of any thread can be preempted at any point

HTML version of Basic Foils prepared May 19 99

Foil 39 Readings

From Elements of Concurrent Programming in Java Tango Group Internal Technology Seminars -- Spring 99. *
Full HTML Index
Concurrent Programming in Java: Design Principles and Patterns by Doug Lea
Java Tutorial: http://www.java.sun.com/docs/books/tutorial/trailmap.html
Java API documentation

© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Wed May 19 1999