240 likes | 322 Views
Threads. Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science. Thread. Definition: A thread is a single sequential flow of control within a program. Introduction. A thread is an instantiation of a code running in a separate time space
E N D
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science
Thread Definition: • A thread is a single sequential flow of control within a program. Computer Science, University of Karlstad
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 Computer Science, University of Karlstad
Introduction A thread is not • a process, it does not have an address space • an OS task, it is part of its parent task Computer Science, University of Karlstad
Multiple Threads can run in a Single Program Computer Science, University of Karlstad
The Basic Idea, Example Producer Consumer Produce something deliver continue Create some need get information continue wait transfer Computer Science, University of Karlstad
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 Computer Science, University of Karlstad
Threads run in Parallel public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start(); } } Computer Science, University of Karlstad
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 Computer Science, University of Karlstad
The Life Cycle of a Thread Computer Science, University of Karlstad
Threads Computer Science, University of Karlstad
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() Computer Science, University of Karlstad
Runnables Computer Science, University of Karlstad
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 Computer Science, University of Karlstad
Thread synchronization: example Public class ThreadApplet extends SequentialApplet { public void start() { new Thread(hello).start(); new Thread(goodbye).start(); } } Computer Science, University of Karlstad
hello 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);} Computer Science, University of Karlstad
hello and goodbye Computer Science, University of Karlstad
Interaction diagrams: Description • 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 Computer Science, University of Karlstad
Interaction diagram: Example applet hello goodbye start start/run appendText start/run appendText return return Computer Science, University of Karlstad
Java synchronization primitives • 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 Computer Science, University of Karlstad
Synchronized wrapper • assume appendText was not already synchronized • could produce • HeGoodlbyelo • GHoeoldlboye • or other funny output instead of • HelloGoodbye (or the other way round) Computer Science, University of Karlstad
Synchronized wrapper class Appender { // page 17 private TextArea textArea; Appender(TextArea t) { textArea = t; } synchronized void append(String s) { textArea.appendText(s); } } Computer Science, University of Karlstad
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) {…} Computer Science, University of Karlstad
Giving 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 Computer Science, University of Karlstad