1 / 24

Threads

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

Télécharger la présentation

Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Thread Definition: • A thread is a single sequential flow of control within a program. Computer Science, University of Karlstad

  3. 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

  4. 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

  5. Multiple Threads can run in a Single Program Computer Science, University of Karlstad

  6. The Basic Idea, Example Producer Consumer Produce something deliver continue Create some need get information continue wait transfer Computer Science, University of Karlstad

  7. 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

  8. 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

  9. 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

  10. The Life Cycle of a Thread Computer Science, University of Karlstad

  11. Threads Computer Science, University of Karlstad

  12. 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

  13. Runnables Computer Science, University of Karlstad

  14. 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

  15. Thread synchronization: example Public class ThreadApplet extends SequentialApplet { public void start() { new Thread(hello).start(); new Thread(goodbye).start(); } } Computer Science, University of Karlstad

  16. 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

  17. hello and goodbye Computer Science, University of Karlstad

  18. 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

  19. Interaction diagram: Example applet hello goodbye start start/run appendText start/run appendText return return Computer Science, University of Karlstad

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

More Related