24
Threads Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

Embed Size (px)

Citation preview

Page 1: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

ThreadsThreads

Eivind J. Nordby

University of Karlstad

Inst. for Information Technology

Dept. of Computer Science

Page 2: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad2

ThreadThread

Definition: A thread is a single sequential flow of

control within a program.

Page 3: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad3

Introduction Introduction

A thread is an instantiation of a code running in a

separate time space is a convenient means for synchronization

of asynchronous tasks is running in the parent process’ address

space

Page 4: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad4

Introduction Introduction

A thread is not a process, it does not have an address

space an OS task, it is part of its parent task

Page 5: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad5

Multiple Threads can run in a Single Program

Page 6: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad6

The Basic Idea, ExampleThe Basic Idea, Example

Create some need

get information

continue

Produce something

deliver

continue

transfer

wait

Producer Consumer

Page 7: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad7

Threads Threads runrunpublic class SimpleThread extends Thread { public SimpleThread(String name) { super(name); } public void run() { 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()); }}

Called when the thread is start()-ed

Page 8: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad8

Threads run in ParallelThreads run in Parallel

public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start(); }}

Page 9: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad9

A Possible ResultA Possible Result

0 Jamaica0 Fiji1 Fiji1 Jamaica2 Jamaica2 Fiji3 Fiji3 Jamaica4 Jamaica4 Fiji5 Jamaica

5 Fiji6 Fiji6 Jamaica7 Jamaica7 Fiji8 Fiji9 Fiji8 JamaicaDONE! Fiji9 JamaicaDONE! Jamaica

Page 10: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad10

The Life Cycle of a Thread

Page 11: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad11

ThreadsThreadslang.Runnable

lang.Thread

+Thread+currentThread:Thread+yield:void+sleep:void+start:void+run:void

SimpleThread

+SimpleThread+run:void

{ super(name); // any other initialization}

{ // do the useful stuff. // When run finishes, // the thread dies}

Page 12: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad12

Implementing the Runnable Interface

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

Applet.start()

Thread.start()

Page 13: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad13

RunnablesRunnables

target

clockThread

lang.Thread

+Thread+currentThread:Thread+start:void+run:void

interfacelang.Runnable

+run:void

Clock

-clockThread:Thread

+Clock+run:void

{ clockThread = new Thread(this, "Clock"); // any other initialization}

{ // do the useful stuff}

{ if (target != null) target.run();}

Page 14: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad14

Implementing the Runnable Interface

public void run() { Thread myThread = Thread.currentThread() while (myThread == clockThread) { repaint(); try { Thread.sleep(1000); } catch (InterruptedException e){} // the VM doesn't want us to sleep // anymore, so get back to work } // My creator has nulled out clockThread // to signal that I should stop working} // run

Page 15: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad15

Thread synchronization: exampleThread synchronization: example

Public class ThreadApplet

extends SequentialApplet {

public void start() {

new Thread(hello).start();

new Thread(goodbye).start();

}

}

Page 16: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad16

hello and goodbyehello and goodbye

are two Runnable objects display hello or goodbye in a common text

area txt use the Java awt function appendText for

the display code page 9

– run() {txt.appendText(msg);}

Page 17: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad17

hello and goodbyehello and goodbye

txt

txt<<instantiate>>

<<instantiate>>

target

target

hello:Messenger[msg="Hello"]

goodbye:Messenger[msg="Goodbye"]

interfacelang.Runnable

+run:void

Messenger:TextArea

:Thread

:Thread

{ txt.appendText(msg);}

Page 18: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad18

Interaction diagrams: Interaction diagrams: DescriptionDescription

Like the GoF patterns, interaction diagrams describe the flow of control

Unlike the GoF patterns the diagrams show discrete steps per thread– no synchronization is implied between the

threads

Page 19: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad19

Interaction diagram: ExampleInteraction diagram: Examplegoodbye applet hello

start start/run

start/run

appendText

appendText

return

return

Page 20: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad20

Java synchronization Java synchronization primitivesprimitives

to avoid interference when more than one concurrent thread can execute a function on a shared object

the keyword synchronized locks out other synchronized threads from that object

Page 21: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad21

Synchronized wrapperSynchronized wrapper

assume appendText was not already synchronized– could produce

HeGoodlbyelo

GHoeoldlboye

– or other funny output instead of Hello

Goodbye (or the other way round)

Page 22: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad22

Synchronized wrapperSynchronized wrapper

class Appender { // page 17 private TextArea textArea; Appender(TextArea t) { textArea = t; } synchronized void append(String s) { textArea.appendText(s); }}

Page 23: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad23

What is synchronized?What is synchronized?

Always an object– the object executing a synchronized method– an explicitly mentioned object

Object syncher = new Object;void f1() {… synchronized(syncher) {…} … }void f2() {… synchronized(syncher) {…} … }

only one block is executed at a time

– a synchroinzed method synchronized on the current object (

synchronized(this) {…}

Page 24: Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science

1998-09-22 Computer Science, University of

Karlstad24

Giving up a synchGiving up a synch

wait suspends the current thread and releases synchronization

another waiting thread synchronized on that object can proceed

the other thread can resume the waiting thread– normally using notifyAll();

one of the waiting threads starts when the lock is freed