Download presentation
Presentation is loading. Please wait.
Published byDavid Wilkinson Modified over 9 years ago
1
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science
2
1998-09-22Computer Science, University of Karlstad2 Thread Definition: l A thread is a single sequential flow of control within a program.
3
1998-09-22Computer Science, University of Karlstad3 Introduction A thread l is an instantiation of a code running in a separate time space l is a convenient means for synchronization of asynchronous tasks l is running in the parent process’ address space
4
1998-09-22Computer Science, University of Karlstad4 Introduction A thread is not l a process, it does not have an address space l an OS task, it is part of its parent task
5
1998-09-22Computer Science, University of Karlstad5 Multiple Threads can run in a Single Program
6
1998-09-22Computer Science, University of Karlstad6 The Basic Idea, Example Create some need get information continue Produce something deliver continue transfer wait ProducerConsumer
7
1998-09-22Computer Science, University of Karlstad7 Threads run public 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
8
1998-09-22Computer Science, University of Karlstad8 Threads run in Parallel public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start(); }
9
1998-09-22Computer Science, University of Karlstad9 A Possible Result 0 Jamaica 0 Fiji 1 Fiji 1 Jamaica 2 Jamaica 2 Fiji 3 Fiji 3 Jamaica 4 Jamaica 4 Fiji 5 Jamaica 5 Fiji 6 Fiji 6 Jamaica 7 Jamaica 7 Fiji 8 Fiji 9 Fiji 8 Jamaica DONE! Fiji 9 Jamaica DONE! Jamaica
10
1998-09-22Computer Science, University of Karlstad10 The Life Cycle of a Thread
11
1998-09-22Computer Science, University of Karlstad11 Threads
12
1998-09-22Computer 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()
13
1998-09-22Computer Science, University of Karlstad13 Runnables
14
1998-09-22Computer 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
15
1998-09-22Computer Science, University of Karlstad15 Thread synchronization: example Public class ThreadApplet extends SequentialApplet { public void start() { new Thread(hello).start(); new Thread(goodbye).start(); }
16
1998-09-22Computer Science, University of Karlstad16 hello and goodbye are two Runnable objects display hello or goodbye in a common text area txt l use the Java awt function appendText for the display l code page 9 –run() {txt.appendText(msg);}
17
1998-09-22Computer Science, University of Karlstad17 hello and goodbye
18
1998-09-22Computer Science, University of Karlstad18 Interaction diagrams: Description l Like the GoF patterns, interaction diagrams describe the flow of control l Unlike the GoF patterns the diagrams show discrete steps per thread –no synchronization is implied between the threads
19
1998-09-22Computer Science, University of Karlstad19 Interaction diagram: Example goodbye applethello start start/run appendText return
20
1998-09-22Computer Science, University of Karlstad20 Java synchronization primitives l 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
21
1998-09-22Computer Science, University of Karlstad21 Synchronized wrapper l assume appendText was not already synchronized –could produce l HeGoodlbye lo l GHoeoldlbo ye –or other funny output instead of l Hello Goodbye (or the other way round)
22
1998-09-22Computer Science, University of Karlstad22 Synchronized wrapper class Appender { // page 17 private TextArea textArea; Appender(TextArea t) { textArea = t; } synchronized void append(String s) { textArea.appendText(s); }
23
1998-09-22Computer Science, University of Karlstad23 What is synchronized? l 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 ( l synchronized(this) {…}
24
1998-09-22Computer Science, University of Karlstad24 Giving up a synch wait suspends the current thread and releases synchronization l another waiting thread synchronized on that object can proceed the other thread can resume the waiting thread –normally using notifyAll(); l one of the waiting threads starts when the lock is freed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.