02/02/2004CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.

Slides:



Advertisements
Similar presentations
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows.
Advertisements

Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads.
5.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts with Java Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
02/05/2008CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying an earlier.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 5: Threads Benefits User and Kernel Threads Multithreading Models Solaris.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
02/05/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading.
CS238 Lecture 5 Threads Dr. Alan R. Davis. Threads Definitions Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java Threads.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
02/02/2004CSCI 315 Operating Systems Design1 Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4 Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4 Threads. SHANDONG UNIVERSITY 2 Contents  Overview  Multithreading Models  Threading Issues  Pthreads  Windows XP Threads  Linux Threads.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Multithreaded Programming Overview.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 4 Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
Chapter 4: Threads. 4.2 Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Multithreading Models Threading Issues Pthreads Solaris 2 Threads.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve Eratosthenes sieve: An algorithm for computing prime numbers:
Chapter 4 Threads Overview This is what a thread has of its own: – A thread id – A program counter – A register set (set of register values) –
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
1 Module 3 - Threads Reading: Chapter 4 Objective:  Understand the concept of the thread and its relationship to the process.  Study and understand the.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Outline n Overview n Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
H.-H. S. Lee 1 ECE3055 Computer Architecture and Operating Systems Lecture 12 Threads Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering.
CSS430 Threads1 Textbook Ch4 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
Chapter 4: Threads Joe McCarthy CSS 430: Operating Systems - Threads1.
CSS430 Threads1 Textbook Ch5 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads (7 th Edition)
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Multithreaded Programming.
CSS 430: Operating Systems - Threads
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Presentation transcript:

02/02/2004CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating Systems Concepts with Java, by Silberschatz, Galvin, and Gagne (2003). Many, if not all, the illustrations contained in this presentation come from this source.

02/02/2004CSCI 315 Operating Systems Design2 Multithreading Models Many-to-One One-to-One Many-to-Many

02/02/2004CSCI 315 Operating Systems Design3 Many-to-One Model k kernel thread user thread Many user-level threads mapped to single kernel thread.

02/02/2004CSCI 315 Operating Systems Design4 One-to-One Model k user threads Each user-level thread maps to kernel thread. kk kernel threads k

02/02/2004CSCI 315 Operating Systems Design5 Many-to-Many Model k kernel threads user threads Several user level threads are mapped to several kernel threads. Allows the operating system to create a sufficient number of kernel threads. kk

02/02/2004CSCI 315 Operating Systems Design6 Two-Level Model k kernel threads user threads Similar to M:M, except that it allows a user thread to be bound to kernel thread. kkk

02/02/2004CSCI 315 Operating Systems Design7 Thread Cancellation Terminating a thread before it has finished. Two general approaches: –Asynchronous cancellation terminates the target thread immediately. –Deferred cancellation allows the target thread to periodically check if it should be cancelled.

02/02/2004CSCI 315 Operating Systems Design8 Thread Specific Data Allows each thread to have its own copy of data. Useful when you do not have control over the thread creation process (i.e., when using a thread pool).

02/02/2004CSCI 315 Operating Systems Design9 Java Threads Java threads are managed by the JVM. Java threads may be created by: –Extending Thread class. –Implementing the Runnable interface.

02/02/2004CSCI 315 Operating Systems Design10 Extending the Thread Class class Worker1 extends Thread { public void run() { System.out.println("I Am a Worker Thread"); } public class First { public static void main(String args[]) { Worker1 runner = new Worker1(); runner.start(); System.out.println("I Am The Main Thread"); }

02/02/2004CSCI 315 Operating Systems Design11 The Runnable Interface public interface Runnable { public abstract void run(); }

02/02/2004CSCI 315 Operating Systems Design12 Implementing the Runnable Interface class Worker2 implements Runnable { public void run() { System.out.println("I Am a Worker Thread"); } public class Second { public static void main(String args[]) { Runnable runner = new Worker2(); Thread thrd = new Thread(runner); thrd.start(); System.out.println("I Am The Main Thread"); }

02/02/2004CSCI 315 Operating Systems Design13 Java Thread States new runnable blocked dead new start() exits run() method sleep() I/O I/O is available

02/02/2004CSCI 315 Operating Systems Design14 Joining Threads class JoinableWorker implements Runnable { public void run() { System.out.println("Worker working"); } public class JoinExample { main(String[] args) { Thread task = new Thread(new JoinableWorker()); task.start(); try { task.join(); } catch (InterruptedException ie) { } System.out.println("Worker done"); }

02/02/2004CSCI 315 Operating Systems Design15 Thread Cancellation Thread thrd = new Thread (new InterruptibleThread()); Thrd.start();... // now interrupt it Thrd.interrupt(); One could also use the stop() method in the thread class, but that is deprecated (that is, still exists, but is being phased out). Note that while stop() is asynchronous cancellation, interrupt() is deferred cancellation.

02/02/2004CSCI 315 Operating Systems Design16 Thread Cancellation public class InterruptibleThread implements Runnable { public void run() { while (true) { /** * do some work for awhile */ if (Thread.currentThread().isInterrupted()) { System.out.println("I'm interrupted!"); break; } // clean up and terminate } With deferred cancellation, the thread must periodically check if it’s been cancelled.

02/02/2004CSCI 315 Operating Systems Design17 Thread-Specific Data All one needs to do in order to create data that is specific to a thread is to subclass the Thread class declaring its own private data. This approach doesn’t work when the developer has no control over the thread creation process.

02/02/2004CSCI 315 Operating Systems Design18 Thread Specific Data class Service { private static ThreadLocal errorCode = new ThreadLocal(); public static void transaction() { try { // some operation where an error may occur catch (Exception e) { errorCode.set(e); } // get the error code for this transaction public static Object getErrorCode() { return errorCode.get(); } write read

02/02/2004CSCI 315 Operating Systems Design19 Thread Specific Data class Worker implements Runnable { private static Service provider; public void run() { provider.transaction(); System.out.println(provider.getErrorCode()); }

02/02/2004CSCI 315 Operating Systems Design20 Producer-Consumer Problem public class Factory { public Factory() { // first create the message buffer Channel mailBox = new MessageQueue(); // now create the producer and consumer threads Thread producerThread = new Thread(new Producer(mailBox)); Thread consumerThread = new Thread(new Consumer(mailBox)); producerThread.start(); consumerThread.start(); } public static void main(String args[]) { Factory server = new Factory(); }

02/02/2004CSCI 315 Operating Systems Design21 Producer Thread class Producer implements Runnable { private Channel mbox; public Producer(Channel mbox) { this.mbox = mbox; } public void run() { Date message; while (true) { SleepUtilities.nap(); message = new Date(); System.out.println("Producer produced " + message); // produce an item & enter it into the buffer mbox.send(message); } send() is non-blocking

02/02/2004CSCI 315 Operating Systems Design22 Consumer Thread class Consumer implements Runnable { private Channel mbox; public Consumer(Channel mbox) { this.mbox = mbox; } public void run() { Date message; while (true) { SleepUtilities.nap(); // consume an item from the buffer System.out.println("Consumer wants to consume."); message = (Date) mbox.receive(); if (message != null) System.out.println("Consumer consumed " + message); } receive() is non-blocking; it may find an empty mailbox