Java and Concurrency - CIS Personal Web Pages

16 downloads 166 Views 176KB Size Report
What do we need for concurrency? • Process creation. – How do you specify concurrent processes? • Communication. – How do processes exchange ... Page 7 ...
CIT Concurrency

Java and Concurrency

What do we need for concurrency? • Process creation

CIT Concurrency

– How do you specify concurrent processes?

• Communication – How do processes exchange information?

• Synchronization – How do processes maintain consistency?

CIT Concurrency

Java Threads (1) • A Thread class manages a single sequential thread of control • Threads may be created and deleted dynamically Thread class MyThread extends Thread { public void run() { //...... } } Thread x = new MyThread();

run()

MyThread run()

CIT Concurrency

Java Threads (2) • Since Java does not permit multiple inheritance, we often implement the run() method in a class not derived from Thread but from the interface Runnable target

Runnable run()

MyRun

Thread public interface Runnable { public abstract void run(); }

class MyRun implements Runnable{ public void run() { run() //..... } } Thread x = new Thread(new MyRun());

CIT Concurrency

java.lang.Thread public class java.lang.Thread extends java.lang.Object implements java.lang.Runnable { public Thread(); public Thread(Runnable target); public Thread(Runnable target, String name); public Thread(String name); public void run(); public synchronized void start(); public static void sleep(long millis) sleep throws InterruptedException; public static void yield(); yield public final String getName(); … }

Java Thread lifecycle (1) new Thread()

start() causes the thread to call its run() method. start()

CIT Concurrency

Created st

Alive

op

stop(), or run() returns ()

Terminated The predicate isAlive() can be used to test if a thread has been started but not terminated. Once terminated, it cannot be restarted

Java Thread lifecycle (2) – Alive start() sl ee su p sp en () d( )

CIT Concurrency

Running yield()

dispatch suspend()

Runnable

Non-Runnable resume() stop(), or run() returns

wait() also makes a Thread Non-Runnable, and notify() Runnable NOTE: suspend(), resume() and stop() are now deprecated

Clock Applet Example (1)

CIT Concurrency

public class Clock extends java.applet.Applet implements Runnable { Thread clockThread = null; public void start() { if (clockThread == null) { clockThread = new Thread(this, "Clock"); clockThread.start() } } ...

Clock Applet Example (2)

CIT Concurrency

... public void run() { // stops when clockThread is set to null while(Thread.currentThread()==clockThread) { repaint(); try { clockThread.sleep(1000); } catch (InterruptedException e){ } } } ...

Clock Applet Example (3) ...

CIT Concurrency

public void paint(Graphics g) { Date now = new Date(); g.drawString(now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds(), 5, 10); } // When the applet stops, stop its thread public void stop() { clockThread = null; } }

Count Down Timer Example (1)

CIT Concurrency

Applet

Runnable

CountDown

init() start() stop() run() tick() beep()

counter display

target

Thread

NumberCanvas setvalue()

CIT Concurrency

Count Down Timer Example (2) public class CountDown extends Applet implements Runnable { Thread counter; int i; final static int N = 10; AudioClip beepSound, tickSound; NumberCanvas display; public void init() public void start() public void stop() public void run() private void tick() private void beep() }

{...} {...} {...} {...} {...} {...}

Count Down Timer Example (3)

CIT Concurrency

public void start() { counter = new Thread(this); i = N; counter.start(); } public void stop() { counter = null; } public void run() while(true) { if (counter if (i>0) { if (i==0) { } }

{ == null) return; tick(); --i; } beep(); return;}

CIT Concurrency

ThreadDemo Example (1)

ThreadDemo Example (2) Panel

Applet

GraphicCanvas

CIT Concurrency

ThreadDemo init() start() stop()

A,B

ThreadPanel

display

rotate() start() stop()

thread

Thread

DisplayThread rotate()

Runnable Rotator run()

target

ThreadDemo Example (3)

CIT Concurrency

class Rotator implements Runnable { public void run() { try { while(true) ThreadPanel.rotate(); } catch(InterruptedException e) {} } } Rotator implements the runnable interface, calling ThreadPanel.rotate() to move the display

run()finishes if an exception is raised by Thread.interrupt().

ThreadDemo Example (4) ThreadPanel public class ThreadPanel extends Panel { manages the display // construct display with title and segment color c and control buttons for public ThreadPanel(String title, Color c) {…} a thread. CIT Concurrency

// rotate display of currently running thread 6 degrees // return value not used in this example Calls to rotate() public static boolean rotate() are delegated to throws InterruptedException {…} DisplayThread. // create a new thread with target r and start it running public void start(Runnable r) { thread = new DisplayThread(canvas,r,…); Threads are created by thread.start(); the start() method, } and terminated by the // stop the thread using Thread.interrupt() stop() method. public void stop() {Thread.interrupt();} }

ThreadDemo Example (5) public class ThreadDemo extends Applet { ThreadPanel A; ThreadPanel B;

CIT Concurrency

public void init() { A = new ThreadPanel("Thread A",Color.blue); B = new ThreadPanel("Thread B",Color.blue); add(A); add(B); ThreadDemo creates two } ThreadPanel displays public void start() { when initialized and two A.start(new Rotator()); threads when started. B.start(new Rotator()); } public void stop() { A.stop(); B.stop(); } }

What do we need for concurrency? • Process creation

CIT Concurrency

– How do you specify concurrent processes?

• Communication – How do processes exchange information?

• Synchronization – How do processes maintain consistency?

Interference (1)

CIT Concurrency

• Ornamental garden problem: – People enter an ornamental garden through either of two turnstiles. Management wish to know how many are in the garden at any time. Garden

West Turnstile

people

East Turnstile

• The concurrent program consists of two concurrent threads and a shared counter object.

Interference (2) Thread

CIT Concurrency

Applet

Garden

east,west

init() go() eastD, westD, counterD

Turnstile run()

people

Counter increment()

display

display

NumberCanvas setvalue()

The Turnstile thread simulates the periodic arrival of a visitor to the garden every second by sleeping for a second and then invoking the increment() method of the counter object.

Interference (3)

CIT Concurrency

• The Counter object and Turnstile threads are created by the go() method of the Garden applet: private void go() { counter = new Counter(counterD); west = new Turnstile(westD,counter); east = new Turnstile(eastD,counter); west.start(); east.start(); }

Interference (4) class Turnstile extends Thread { NumberCanvas display; Counter people; CIT Concurrency

Turnstile(NumberCanvas n,Counter c) The run() method { display = n; people = c; } exits and the thread public void run() { terminates after try{ Garden.MAX visitors display.setvalue(0); have entered. for (int i=1;i