1 / 35

Chapter 5 Threads

Chapter 5 Threads. Contents. Overview Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java Threads. Thread. § 5.1. A thread, sometimes called a lightweight process , is a basic unit of CPU utilization Comprises A thread ID A program counter A register set

cliff
Télécharger la présentation

Chapter 5 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. Chapter 5 Threads

  2. Contents • Overview • Benefits • User and Kernel Threads • Multithreading Models • Solaris 2 Threads • Java Threads

  3. Thread § 5.1 • A thread, sometimes called a lightweight process, is a basic unit of CPU utilization • Comprises • A thread ID • A program counter • A register set • A stack • Shares with other threads belonging to the same process: • Code section • Data section • Other resources

  4. Thread • A traditional, or heavyweight, process has a single thread of control.

  5. Thread Examples • Busy web server has many concurrently accessing clients. • Java program connecting to the server will be blocked until the connection is made since Java has no concept of asynchronous. Solution: another thread with timeout sleeping checking the connecting thread when wake up.

  6. Benefits § 5.2 • Responsiveness– continue running even if part of it is blocked • Resource sharing– allows an application to have several different threads of activity all within the same address space • Economy– it is more economical to create and context switch threads than process • Utilization of multiprocessor architectures– in single-processor architecture, multithread is only an illusion

  7. User Threads § 5.3 • User threads are supported by user-level threads library without the need for kernel intervention. • Fast to create and manage. • Examples - POSIX Pthreads - Mach C-threads - Solaris threads

  8. Kernel Threads § 5.3.2 • Kernel threads are supported directly by the OS in kernel space • Slower to create and manage. • Examples - Windows 95/98/NT - Solaris - Digital UNIX

  9. Multithreading Models § 5.4 • Many-to-One Model • One-to-One Model • Many-to-Many Model

  10. Many-to-One Model • Many user-level threads mapped to single kernel thread. • Used on systems that do not support multiple kernel threads. • Efficient, but the entire process will be blocked if a thread makes a blocking system call • Since only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors

  11. Many-to-One Model

  12. One-to-One Model • Each user thread maps to a kernel thread. • Provides more concurrency than the many-to-one • Drawback: creating a user thread requires creating the corresponding kernel thread … burden the performance … most system restrict the number of threads supported. • Examples - Windows 95/98/NT - OS/2

  13. One-to-One Model

  14. Many-to-Many Model • Multiplex many user threads to a smaller or equal number of kernel threads. • Developers can create as many user threads as necessary and the corresponding kernel threads can run in parallel on a multiprocessor. • Examples: • Solaris • IRIX • Digital UNIX

  15. Many-to-Many Model

  16. Solaris 2 Threads § 5.5 • Solaris 2 is a version of UNIX • Supported only traditional heavy-weight processes within a single thread of control until 1992 • Intermediate level thread: lightweight processes (LWP) • Each process contains at least one LWP • Only user-level threads currently connected to an LWP accomplish work

  17. Solaris 2 Threads

  18. Solaris Process (略)

  19. Java Threads § 5.6 • Java is unique because it provides support for creation and management of threads at the language level, it provides commands that allows the programmer to create and manipulate threads of control within the program. • All Java program comprise at least a single thread of control. • Java Threads May be Created by: • Extending Thread class • Implementing the Runnable interface

  20. Extending the Thread Class Thread Worker1 run 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”); } } runner Worker1 start

  21. Extending the Thread Class • An object of this derived class will run as a separate thread of control in the JVM. • Two threads are created by the JVM: • The thread associated with the application – the thread that starts execution at the main() method. • The runner thread that is created explicitly with the start() method.

  22. 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[]) { Worker runner = new Worker1(); runner.start(); System.out.println(“I am the main thread”); } } 1 2

  23. The Runnable Interface • Another option to create a separate thread is to define a class that implements the Runnable interface. • The runnable interface is defined as public interface Runnable{ public abstract void run();} • When a class implements Runnable, it must define a run() method.

  24. 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”); } }

  25. Why Implements Runnable ? • Since Java does not support multiple inheritance, if a class is already derived from another class, it will not also be able to extend the Thread class. • Example: an applet already extends the Applet class has to also implement the Runnable interface: public class ThreadedApplet extends Applet implements Runnable { ……… } • Unless a class requires multithreading and is already extended, we will adopt the approach of extending the Thread class.

  26. Java Thread Management § 5.6.2 • Java APIs for managing threads: • suspend()– suspends execution of the currently running thread. • sleep()– puts the currently running thread to sleep for a specified amount of time. • resume()– resumes execution of a suspended thread. • stop()– stops execution of a thread.

  27. Applets with threads • Applets are natural examples for multithreading because they commonly have graphics, animation, and audio – all good candidates as threads. • start() method of an applet is called when an applet is first displayed. If the user leaves the web page or the applet scrolls off the screen, the applet’s stop() method is called. • The destroy() method of an applet is called when the applet is removed from the browser’s cache.

  28. Java Thread States § 5.6.3 • A Java thread can be in one of four states: • New: when an object for the thread is created • Runnable: When a thread’s run() method is invoked, the thread moves from the New to the Runnable state. • Blocked: when performing a blocking statement, such as doing I/O, or if it invokes methods like sleep() or suspend(). • Dead: when its run() method terminates or when its stop() method is called.

  29. Java Thread States • It is not possible to determine the exact state of a thread, however, isAlive() method returns a boolean value to determine whether or not a thread is dead.

  30. Threads and the JVM § 5.6.4 • In addition to a Java application program contains several different threads of control, there are several threads running asynchronously on behalf of the JVM handling system-level tasks such as memory management and graphics control.

  31. JVM and Host OS § 5.6.5 • The typical implementation of the JVM is on top of a host OS. This allows the JVM to hide the implementation details of the underlying OS and to provide a consistent, abstract environment that allows Java programs to operate on any platform that supports a JVM. • Windows NT: one-to-one, each Java thread for a JVM running on NT maps to a kernel thread • Solaris 2: many-to-one • Solaris 2.6: many-to-many

  32. Producer Consumer Problem public class Server { public Server() { // first create the message buffer MessageQueue mailBox = new MessageQueue(); // now create the producer and consumer threads Producer producerThread = new Producer(mailBox); Consumer consumerThread = new Consumer(mailBox); producerThread.start(); consumerThread.start(); } public static void main(String args[]) { Server server = new Server(); } }

  33. queue vector Producer Consumer Problem consumerThread producerThread producer consumer mailBox send receive MessageQueue Server

  34. queue vector Producer Thread class Producer extends Thread { public Producer(MessageQueue m) { mbox = m; } public void run() { while (true) { // produce an item & enter it into the buffer Date message = new Date(); mbox.send(message); } } private MessageQueue mbox; } Thread Producer mBox send receive MessageQueue

  35. queue vector Consumer Thread class Consumer extends Thread { public Consumer(MessageQueue m) { mbox = m; } public void run() { while (true) { Date message = (Date)mbox.receive(); if (message != null) // consume the message } } private MessageQueue mbox; } Thread Consumer mBox send receive MessageQueue

More Related