1 Chapter 5 Threads 2 Contents Overview Benefits User and Kernel Threads Multithreading Models ...

35
1 Chapter 5 Threads

Transcript of 1 Chapter 5 Threads 2 Contents Overview Benefits User and Kernel Threads Multithreading Models ...

Page 1: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

1

Chapter 5 Threads

Page 2: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

2

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

Page 3: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

3

Thread A thread, sometimes called a lightweight

process, is a basic unit of CPU utilization Comprises

A thread IDA program counterA register setA stack

Shares with other threads belonging to the same process:Code sectionData sectionOther resources

§ 5.1

Page 4: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

4

Thread A traditional, or heavyweight, process has a

single thread of control.

Page 5: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

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.

Page 6: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

6

Benefits 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

§ 5.2

Page 7: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

7

User Threads 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

§ 5.3

Page 8: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

8

Kernel Threads Kernel threads are supported directly by the OS

in kernel space Slower to create and manage. Examples

- Windows 95/98/NT

- Solaris

- Digital UNIX

§ 5.3.2

Page 9: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

9

Multithreading Models Many-to-One Model

One-to-One Model

Many-to-Many Model

§ 5.4

Page 10: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

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

Page 11: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

11

Many-to-One Model

Page 12: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

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

Page 13: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

13

One-to-One Model

Page 14: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

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:SolarisIRIXDigital UNIX

Page 15: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

15

Many-to-Many Model

Page 16: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

16

Solaris 2 Threads 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

§ 5.5

Page 17: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

17

Solaris 2 Threads

Page 18: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

18

Solaris Process (略 )

Page 19: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

19

Java Threads 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

§ 5.6

Page 20: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

20

Extending the Thread Classclass 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”);}

}

Worker1Thread

Worker1

runner

run

start

Page 21: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

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.

Page 22: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

22

Extending the Thread Classclass 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

Page 23: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

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 aspublic interface Runnable{

public abstract void run();}

When a class implements Runnable, it must define a run() method.

Page 24: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

24

Implementing the Runnable Interfaceclass 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”);}

}

Page 25: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

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.

Page 26: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

26

Java Thread Management 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.

§ 5.6.2

Page 27: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

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.

Page 28: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

28

Java Thread States A Java thread can be in one of four states:

New: when an object for the thread is createdRunnable: 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.

§ 5.6.3

Page 29: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

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.

Page 30: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

30

Threads and the JVM 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.

§ 5.6.4

Page 31: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

31

JVM and Host OS 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

§ 5.6.5

Page 32: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

32

Producer Consumer Problempublic class Server {

public Server() {// first create the message bufferMessageQueue 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(); }}

Page 33: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

33

Producer Consumer Problem

MessageQueue

send receive

vector

queue

mailBox

consumerproducer

producerThread consumerThread

Server

Page 34: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

34

Producer Threadclass 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;}

Producer MessageQueue

send receive

vector

queue

mBoxThread

Page 35: 1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

35

Consumer Threadclass 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;}

Consumer MessageQueue

send receive

vector

queue

mBoxThread