NORDBY CONSTRUCTION, INC. v. AMERICAN SAFETY INDEMNITY COMPANY et al complaint
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer...
-
Upload
david-wilkinson -
Category
Documents
-
view
213 -
download
0
Transcript of Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer...
ThreadsThreads
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.
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
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
1998-09-22 Computer Science, University of
Karlstad5
Multiple Threads can run in a Single Program
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
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
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(); }}
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
1998-09-22 Computer Science, University of
Karlstad10
The Life Cycle of a Thread
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}
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()
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();}
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
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();
}
}
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);}
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);}
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
1998-09-22 Computer Science, University of
Karlstad19
Interaction diagram: ExampleInteraction diagram: Examplegoodbye applet hello
start start/run
start/run
appendText
appendText
return
return
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
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)
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); }}
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) {…}
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