Lecturer: Prof. O. Nierstrasz
Schützenmattstr. 14/103, Tel. 631.4618
Secr.: Frau I. Huber, Tel. 631.4692
"A sequential program specifies sequential execution of a list of statements; its execution is called a process. A concurrent program specifies two or more sequential programs that may be executed concurrently as parallel processes."
A concurrent program can be executed by:
Assume only that all processes make positive finite progress.
NB: Sometimes we distinguish between a process, which has its own address space provided by the operating system, and a thread, which shares an address space with other threads. Thus, a process may be multi-threaded. Here we consider threads and processes to be the same thing ...
Programs P1 and P2 execute concurrently:
What are possible values of x after P1 and P2 complete?
What is the intended final value of x?
Synchronization mechanisms are needed to restrict the possible interleavings of processes so that sets of actions can be seen as atomic.
Mutual exclusion ensures that statements within a critical section are treated atomically.
Notations for expressing concurrent computation must address:
Idioms, patterns and architectural styles express best practice in resolving common design problems.
Examples: function objects, orthodox canonical form, futures, RPC
Language design influenced by existing OO languages (C++, Smalltalk ...):
Innovation in support for network applications:
A Thread defines its behaviour in its run method, but is started by calling start() :
// Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
class TwoThreadsTest {
public static void main (String[] args) {
new SimpleThread("Jamaica").start(); // Instantiate, then start
new SimpleThread("Fiji").start();
}
}
class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str); // Call Thread constructor
}
public void run() { // What the thread does
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) { }
}
System.out.println("DONE! " + getName());
}
}
The Thread class encapsulates all information concerning running threads of control:
public class java.lang.Thread
extends java.lang.Object implements java.lang.Runnable
{
public Thread(); // Public constructors
public Thread(Runnable target);
public Thread(Runnable target, String name);
public Thread(String name);
...
public static void sleep(long millis) // Current thread sleeps
throws InterruptedException;
public static void yield(); // Yield control (equal priority)
...
public final String getName();
public void run(); // "main()" method
public synchronized void start(); // Starts a thread running
public final void suspend(); // Temporarily halts a thread
public final void resume(); // Allow to resume after suspend()
public final void stop(); // Throws a ThreadDeath error
public final void join() // Waits for thread to die
throws InterruptedException;
...
}