5.1Applied Operating System Concepts
Module 5,Threads线程
Overview
综述
Benefits
益处
User and Kernel Threads
用户和内核线程
Multithreading Models
多线程模型
Solaris 2 Threads
Solaris 2线程
Java Threads
Java线程
5.2Applied Operating System Concepts
Responsiveness
响应
Resource Sharing
资源共享
Economy
经济性
Utilization of MP Architectures
MP体系结构的运用
Benefits益处
5.3Applied Operating System Concepts
Single and Multithreaded Processes
单个和多线程进程
5.4Applied Operating System Concepts
User Threads
用户线程
Thread Management Done by User-Level Threads Library
由用户级线程库进行管理的线程
Examples例子
- POSIX Pthreads
- Mach C-threads
- Solaris threads
5.5Applied Operating System Concepts
Kernel Threads
内核线程
Supported by the Kernel
由内核支持
Examples例子
- Windows 95/98/NT
- Solaris
- Digital UNIX
5.6Applied Operating System Concepts
Multithreading Models
多线程模型
Many-to-One
多对一
One-to-One
一对一
Many-to-Many
多对多
5.7Applied Operating System Concepts
Many-to-One多对一
Many User-Level Threads Mapped to Single Kernel Thread.
多个用户级线程映像进单个内核线程
Used on Systems That Do Not Support Kernel Threads.
用于不支持内核线程的系统中
5.8Applied Operating System Concepts
Many-to-one Model多对一模型
5.9Applied Operating System Concepts
One-to-One一对一
Each User-Level Thread Maps to Kernel Thread.
每个用户级线程映像进内核线程
Examples
- Windows 95/98/NT
- OS/2
5.10Applied Operating System Concepts
One-to-one Model一对一模型
5.11Applied Operating System Concepts
Many-to-many Model多对多模型
5.12Applied Operating System Concepts
Solaris 2 Threads
Solaris 2线程
5.13Applied Operating System Concepts
Solaris Process
Solaris 线程
5.14Applied Operating System Concepts
Java Threads
Java线程
Java Threads May be Created by:
Java线程可如下创建:
– Extending Thread class
扩充线程类
– Implementing the Runnable interface
实现可运行接口
5.15Applied Operating System Concepts
Extending the Thread Class
线程类型的扩展
class Worker1 extends Thread
{
public void run() {
System.out.println(“I am a Worker Thread”);
}
}
5.16Applied Operating System Concepts
Creating the Thread
创建线程
public class First
{
public static void main(String args[]) {
Worker runner = new Worker1();
runner.start();
System.out.println(“I am the main thread”);
}
}
5.17Applied Operating System Concepts
The Runnable Interface
可运行接口
public interface Runnable
{
public abstract void run();
}
5.18Applied Operating System Concepts
Implementing the Runnable Interface
可运行接口的实现
class Worker2 implements Runnable
{
public void run() {
System.out.println(“I am a Worker Thread”);
}
}
5.19Applied Operating System Concepts
Creating the 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”);
}
}
5.20Applied Operating System Concepts
Java Thread Management
Java线程的管理
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.21Applied Operating System Concepts
Java Thread States
Java线程状态
5.22Applied Operating System Concepts
Producer Consumer Problem
生产着消费者问题
public class Server {
public Server() {
MessageQueue mailBox = new MessageQueue();
Producer producerThread = new Producer(mailBox);
Consumer consumerThread = new Consumer(mailBox);
producerThread.start();
consumerThread.start();
}
public static void main(String args[]) {
Server server = new Server();
}
}
5.23Applied Operating System Concepts
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;
}
5.24Applied Operating System Concepts
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;
}