1 / 35

Chapter 4: Threads

Chapter 4: Threads. Chapter 4: Threads. Overview Multithreading Models Threading Issues Operating System Examples. Objectives. To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems

mrossi
Download Presentation

Chapter 4: 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 4: Threads

  2. Chapter 4: Threads Overview Multithreading Models Threading Issues Operating System Examples

  3. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems To examine issues related to multithreaded programming

  4. Single and Multithreaded Processes

  5. Threads Threads within a process, share the code, data, and heap sections but have their own stack segments. Threads share static variables and instance variables of an object. Threads also have their own copies of CPU registers like the Program Counter.

  6. Threads vs. Processes Threads allow computation to be overlapped with I/O and, in the case of a multiprocessor, with other computation. The same overlap could be achieved through the use of multiple single-threaded processes. Why, then, should the multi-threaded process model be preferred? Threads are cheaper to create and manage than processes. Resource sharing can be achieved more efficiently between threads than between processes because threads share data segments. Thus they share instance and static variables of an object if there are more than thread per object.

  7. Threads vs. Processes 3. Context switching between to a different thread within the same process is cheaper than switching between threads belonging to different processes (need to switch to a different memory segments and data and code segments are different and caches will have more misses till old instruction/data are flushed out.) 4. But, by the same token, threads within a process are not protected from one another. So programmers need to ensure that critical sections of the code are kept thread safe using primitives such as the Synchronized blocks in Java.

  8. Threads vs. Processes The overheads associated with creating a process are in general considerably greater than those of creating a new thread. A new execution environment must first be created, including address space tables. As an example, Anderson et al quote a figure of about 11 milliseconds to create a new UNIX process, and about 1 millisecond to create a thread on the same CVAX processor architecture running the Topaz kernel. Figures quoted by Anderson et al are 1.8 milliseconds for the Topaz kernel to switch between UNIX processes and 0.4 milliseconds to switch between threads belonging to the same execution environment (since threads share the address space, only CPU registers and Program Counter need to be saved/restored).

  9. Benefits Performance Improved throughput. Many concurrent compute operations and I/O requests within a single process. Simultaneous use of multiple processors for computation and I/O. Improved server responsiveness. Large or complex requests or slow clients don't block other requests for service. The overall throughput of the server is much greater. Resource Sharing Sharing large amounts of data through separate threads of execution within the same address space provides high-bandwidth, low-latency communication between separate tasks within an application. Minimal impact on system resources Threads impose minimal impact on system resources. Threads require less overhead to create, maintain, and manage than a traditional process.

  10. Multithreaded Server Architecture

  11. Concurrent Execution on a Single-core System

  12. Parallel Execution on a Multicore System

  13. Thread Lifetimes The Java Thread class includes the constructor and management methods listed in the next slide A new thread is created on the same Java virtual machine (JVM) as its creator, in the SUSPENDED state. After it is made RUNNABLE with the start() method, it executes the run() method of an object designated in its constructor. The JVM and the threads on top of it all execute in a process on top of the underlying operating system. Threads can be assigned a priority, so that a Java implementation that supports priorities will run a particular thread in preference to any thread with lower priority. A thread ends its life when it returns from the run() method or when its destroy() method is called.

  14. Java Thread Class

  15. Java Thread Life Cycle

  16. Java Thread Life Cycle New: When instance of thread is created using new operator it is in new state, but the start() method has not been invoked on the thread yet, thread is not eligible to run yet. Runnable: When start() method is called on thread it enters runnable state. As soon as Thread enters runnable state it is eligible to run, but not running. (Thread scheduler has not scheduled the Thread execution yet.) Running: Thread scheduler selects thread to go from runnable to running state. In running state Thread starts executing by entering run() method.

  17. Java Thread Life Cycle Waiting/Blocked/sleeping: In this state a thread is not eligible to run. By calling wait() method thread go from running to waiting state. In waiting state it will wait for other threads to release object monitor/lock. Once notify() or notifyAll() method is called object monitor/lock becomes available and thread can again return to runnable state. By calling sleep() method thread go from running to sleeping state. In sleeping state it will wait for sleep time to get over. Once specified sleep time is up thread can again return to runnable state. Suspend() method can be used to put thread in waiting state and resume() method is the only way which could put thread in runnable state. Terminated (Dead): A thread is considered dead when its run() method completes. Once thread is dead it cannot be started again doing so will throw runtimeException i.e. IllegalThreadStateException. destroy() method puts thread directly into dead state.

  18. Thread Synchronization Programming a multi-threaded process requires great care. The main difficult issues are the sharing of objects and the techniques used for thread coordination and cooperation. Each thread’s local variables in methods are private to it – threads have private stacks. However, threads are not given private copies of static (class) variables or object instance variables. Thread synchronization is needed to as two threads may try to alter a shared instance or static variable at the same time. Java allows threads to be blocked and woken up via arbitrary objects that act as condition variables. A thread that needs to block awaiting a certain condition calls an object’s wait() method.

  19. Thread Synchronization Another thread calls notify() to unblock at most one thread or notifyAll() to unblock all threads waiting on that object. As an example, when a worker thread discovers that there are no requests to process, it calls wait() on the instance of Queue. When the I/O thread subsequently adds a request to the queue, it calls the queue’s notify() method to wake up a worker.

  20. Thread Implementation Many kernels provide native support for multi-threaded processes, including Windows, Linux, Solaris, Mach and Mac OS X. These kernels provide thread-creation and -management system calls, and they schedule individual threads. Some other kernels have only a single-threaded process abstraction. Multithreaded processes must then be implemented in a library of procedures linked to application programs. In such cases, the kernel has no knowledge of these user-level threads and therefore cannot schedule them independently. A threads runtime library organizes the scheduling of threads. A thread would block the process, and therefore all threads within it, if it made a blocking system call.

  21. Thread Implementation Disadvantages of not having kernel/native support for threads: The threads within a process cannot take advantage of a multiprocessor. A thread that makes a page fault blocks the entire process and all threads within it. A thread would block the process, and therefore all threads within it if made a blocking I/O system call. Threads within different processes cannot be scheduled according to a single scheme of relative prioritization. E.g. the OS can preempt a native thread at any time, say by using the Round-Robin scheduling policy, and run some other thread that was not currently running.

  22. Kernel Threads Supported by the Kernel Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X

  23. Multithreading Models Many-to-One One-to-One

  24. Many-to-One Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads

  25. Many-to-One Model

  26. One-to-One Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later

  27. One-to-one Model

  28. Java Threads • Java threads are managed by the JVM • Java threads may be created by: • Implementing the Runnable interface

  29. Java Threads - Example Program

  30. Java Threads - Example Program

  31. Java Thread States

  32. Java Threads - Producer-Consumer

  33. Java Threads - Producer-Consumer

  34. Java Threads - Producer-Consumer

  35. Thread Pools Create a number of threads in a pool where they await work. Advantages: Usually slightly faster to service a request with an existing thread than create a new thread. Allows the number of threads in the application(s) to be bound to the size of the pool.

More Related