SlideShare a Scribd company logo
1 of 24
Silberschatz, Galvin, and Gagne ©19995.1
Module 5: Threads
• Benefits
• User and Kernel Threads
• Multithreading Models
• Solaris 2 Threads
• Java Threads
Silberschatz, Galvin, and Gagne ©19995.2
• Responsiveness
• Resource Sharing
• Economy
• Utilization of MP Architectures
Benefits
Silberschatz, Galvin, and Gagne ©19995.3
Single and Multithreaded Processes
Silberschatz, Galvin, and Gagne ©19995.4
User Threads
• Thread Management Done by User-Level Threads Library
• Examples
- POSIX Pthreads
- Mach C-threads
- Solaris threads
Silberschatz, Galvin, and Gagne ©19995.5
Kernel Threads
• Supported by the Kernel
• Examples
- Windows 95/98/NT
- Solaris
- Digital UNIX
Silberschatz, Galvin, and Gagne ©19995.6
Multithreading Models
• Many-to-One
• One-to-One
• Many-to-Many
Silberschatz, Galvin, and Gagne ©19995.7
Many-to-One
• Many User-Level Threads Mapped to Single Kernel Thread.
• Used on Systems That Do Not Support Kernel Threads.
Silberschatz, Galvin, and Gagne ©19995.8
Many-to-one Model
Silberschatz, Galvin, and Gagne ©19995.9
One-to-One
• Each User-Level Thread Maps to Kernel Thread.
• Examples
- Windows 95/98/NT
- OS/2
Silberschatz, Galvin, and Gagne ©19995.10
One-to-one Model
Silberschatz, Galvin, and Gagne ©19995.11
Many-to-many Model
Silberschatz, Galvin, and Gagne ©19995.12
Solaris 2 Threads
Silberschatz, Galvin, and Gagne ©19995.13
Solaris Process
Silberschatz, Galvin, and Gagne ©19995.14
Java Threads
• Java Threads May be Created by:
– Extending Thread class
– Implementing the Runnable interface
Silberschatz, Galvin, and Gagne ©19995.15
Extending the Thread Class
class Worker1 extends Thread
{
public void run() {
System.out.println(“I am a Worker Thread”);
}
}
Silberschatz, Galvin, and Gagne ©19995.16
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”);
}
}
Silberschatz, Galvin, and Gagne ©19995.17
The Runnable Interface
public interface Runnable
{
public abstract void run();
}
Silberschatz, Galvin, and Gagne ©19995.18
Implementing the Runnable
Interface
class Worker2 implements Runnable
{
public void run() {
System.out.println(“I am a Worker Thread”);
}
}
Silberschatz, Galvin, and Gagne ©19995.19
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”);
}
}
Silberschatz, Galvin, and Gagne ©19995.20
Java Thread Management
• 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.
Silberschatz, Galvin, and Gagne ©19995.21
Java Thread States
Silberschatz, Galvin, and Gagne ©19995.22
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();
}
}
Silberschatz, Galvin, and Gagne ©19995.23
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;
}
Silberschatz, Galvin, and Gagne ©19995.24
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;
}

More Related Content

What's hot

OpenNebulaConf 2016 - OpenNebula 5.0 Highlights and Beyond by Ruben S. Monter...
OpenNebulaConf 2016 - OpenNebula 5.0 Highlights and Beyond by Ruben S. Monter...OpenNebulaConf 2016 - OpenNebula 5.0 Highlights and Beyond by Ruben S. Monter...
OpenNebulaConf 2016 - OpenNebula 5.0 Highlights and Beyond by Ruben S. Monter...OpenNebula Project
 
Cncf meetup kubespray
Cncf meetup kubesprayCncf meetup kubespray
Cncf meetup kubesprayJuraj Hantak
 
Introducing OpenStack for Beginners
Introducing OpenStack for Beginners Introducing OpenStack for Beginners
Introducing OpenStack for Beginners openstackindia
 
OpenNebula - Key Aspects in CentOS
OpenNebula - Key Aspects in CentOSOpenNebula - Key Aspects in CentOS
OpenNebula - Key Aspects in CentOSOpenNebula Project
 
TechDay - Toronto 2016 - OpenNebula @ Fuze
TechDay - Toronto 2016 - OpenNebula @ FuzeTechDay - Toronto 2016 - OpenNebula @ Fuze
TechDay - Toronto 2016 - OpenNebula @ FuzeOpenNebula Project
 
Disaster recovery solution with open nebula and storpool
Disaster recovery solution with open nebula and storpoolDisaster recovery solution with open nebula and storpool
Disaster recovery solution with open nebula and storpoolOpenNebula Project
 

What's hot (8)

OpenNebulaConf 2016 - OpenNebula 5.0 Highlights and Beyond by Ruben S. Monter...
OpenNebulaConf 2016 - OpenNebula 5.0 Highlights and Beyond by Ruben S. Monter...OpenNebulaConf 2016 - OpenNebula 5.0 Highlights and Beyond by Ruben S. Monter...
OpenNebulaConf 2016 - OpenNebula 5.0 Highlights and Beyond by Ruben S. Monter...
 
Cncf meetup kubespray
Cncf meetup kubesprayCncf meetup kubespray
Cncf meetup kubespray
 
Introducing OpenStack for Beginners
Introducing OpenStack for Beginners Introducing OpenStack for Beginners
Introducing OpenStack for Beginners
 
OpenNebula - Key Aspects in CentOS
OpenNebula - Key Aspects in CentOSOpenNebula - Key Aspects in CentOS
OpenNebula - Key Aspects in CentOS
 
TechDay - Toronto 2016 - OpenNebula @ Fuze
TechDay - Toronto 2016 - OpenNebula @ FuzeTechDay - Toronto 2016 - OpenNebula @ Fuze
TechDay - Toronto 2016 - OpenNebula @ Fuze
 
OpenNebula - The Project
OpenNebula - The ProjectOpenNebula - The Project
OpenNebula - The Project
 
Disaster recovery solution with open nebula and storpool
Disaster recovery solution with open nebula and storpoolDisaster recovery solution with open nebula and storpool
Disaster recovery solution with open nebula and storpool
 
Docker for HPC in a Nutshell
Docker for HPC in a NutshellDocker for HPC in a Nutshell
Docker for HPC in a Nutshell
 

Similar to Operating System - Ch5

Ch5: Threads (Operating System)
Ch5: Threads (Operating System)Ch5: Threads (Operating System)
Ch5: Threads (Operating System)Ahmar Hashmi
 
Operating System : Ch4 b threads
Operating System : Ch4 b threadsOperating System : Ch4 b threads
Operating System : Ch4 b threadsSyaiful Ahdan
 
Operating System : Ch16.distributed system structures
Operating System : Ch16.distributed system structuresOperating System : Ch16.distributed system structures
Operating System : Ch16.distributed system structuresSyaiful Ahdan
 
Operating System-Ch8 memory management
Operating System-Ch8 memory managementOperating System-Ch8 memory management
Operating System-Ch8 memory managementSyaiful Ahdan
 
Operating System : Ch18 distributed coordination
Operating System : Ch18 distributed coordinationOperating System : Ch18 distributed coordination
Operating System : Ch18 distributed coordinationSyaiful Ahdan
 
Operating System : Ch15 network structures
Operating System : Ch15 network structuresOperating System : Ch15 network structures
Operating System : Ch15 network structuresSyaiful Ahdan
 
Operating System : Ch19 protection
Operating System :  Ch19 protectionOperating System :  Ch19 protection
Operating System : Ch19 protectionSyaiful Ahdan
 
Galvin-operating System(Ch5)
Galvin-operating System(Ch5)Galvin-operating System(Ch5)
Galvin-operating System(Ch5)dsuyal1
 
Operating System : Ch17 distributed file systems
Operating System : Ch17 distributed file systemsOperating System : Ch17 distributed file systems
Operating System : Ch17 distributed file systemsSyaiful Ahdan
 
Operating System-Ch7 deadlocks
Operating System-Ch7 deadlocksOperating System-Ch7 deadlocks
Operating System-Ch7 deadlocksSyaiful Ahdan
 
Operating System : Ch12 io systems
Operating System : Ch12 io systemsOperating System : Ch12 io systems
Operating System : Ch12 io systemsSyaiful Ahdan
 

Similar to Operating System - Ch5 (20)

Ch5 threads galvin
Ch5 threads  galvinCh5 threads  galvin
Ch5 threads galvin
 
Ch5: Threads (Operating System)
Ch5: Threads (Operating System)Ch5: Threads (Operating System)
Ch5: Threads (Operating System)
 
Operating System : Ch4 b threads
Operating System : Ch4 b threadsOperating System : Ch4 b threads
Operating System : Ch4 b threads
 
Ch4 threads
Ch4  threadsCh4  threads
Ch4 threads
 
Operating System : Ch16.distributed system structures
Operating System : Ch16.distributed system structuresOperating System : Ch16.distributed system structures
Operating System : Ch16.distributed system structures
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Ch4 threads
Ch4 threadsCh4 threads
Ch4 threads
 
Operating System-Ch8 memory management
Operating System-Ch8 memory managementOperating System-Ch8 memory management
Operating System-Ch8 memory management
 
Materi8mainmemory
Materi8mainmemoryMateri8mainmemory
Materi8mainmemory
 
Ch8 main memory
Ch8 main memoryCh8 main memory
Ch8 main memory
 
Operating System : Ch18 distributed coordination
Operating System : Ch18 distributed coordinationOperating System : Ch18 distributed coordination
Operating System : Ch18 distributed coordination
 
Operating System : Ch15 network structures
Operating System : Ch15 network structuresOperating System : Ch15 network structures
Operating System : Ch15 network structures
 
Operating System : Ch19 protection
Operating System :  Ch19 protectionOperating System :  Ch19 protection
Operating System : Ch19 protection
 
Ch14 protection
Ch14 protectionCh14 protection
Ch14 protection
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
Galvin-operating System(Ch5)
Galvin-operating System(Ch5)Galvin-operating System(Ch5)
Galvin-operating System(Ch5)
 
Operating System : Ch17 distributed file systems
Operating System : Ch17 distributed file systemsOperating System : Ch17 distributed file systems
Operating System : Ch17 distributed file systems
 
Operating System-Ch7 deadlocks
Operating System-Ch7 deadlocksOperating System-Ch7 deadlocks
Operating System-Ch7 deadlocks
 
Ch7 deadlocks
Ch7 deadlocksCh7 deadlocks
Ch7 deadlocks
 
Operating System : Ch12 io systems
Operating System : Ch12 io systemsOperating System : Ch12 io systems
Operating System : Ch12 io systems
 

Recently uploaded

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 

Recently uploaded (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 

Operating System - Ch5

  • 1. Silberschatz, Galvin, and Gagne ©19995.1 Module 5: Threads • Benefits • User and Kernel Threads • Multithreading Models • Solaris 2 Threads • Java Threads
  • 2. Silberschatz, Galvin, and Gagne ©19995.2 • Responsiveness • Resource Sharing • Economy • Utilization of MP Architectures Benefits
  • 3. Silberschatz, Galvin, and Gagne ©19995.3 Single and Multithreaded Processes
  • 4. Silberschatz, Galvin, and Gagne ©19995.4 User Threads • Thread Management Done by User-Level Threads Library • Examples - POSIX Pthreads - Mach C-threads - Solaris threads
  • 5. Silberschatz, Galvin, and Gagne ©19995.5 Kernel Threads • Supported by the Kernel • Examples - Windows 95/98/NT - Solaris - Digital UNIX
  • 6. Silberschatz, Galvin, and Gagne ©19995.6 Multithreading Models • Many-to-One • One-to-One • Many-to-Many
  • 7. Silberschatz, Galvin, and Gagne ©19995.7 Many-to-One • Many User-Level Threads Mapped to Single Kernel Thread. • Used on Systems That Do Not Support Kernel Threads.
  • 8. Silberschatz, Galvin, and Gagne ©19995.8 Many-to-one Model
  • 9. Silberschatz, Galvin, and Gagne ©19995.9 One-to-One • Each User-Level Thread Maps to Kernel Thread. • Examples - Windows 95/98/NT - OS/2
  • 10. Silberschatz, Galvin, and Gagne ©19995.10 One-to-one Model
  • 11. Silberschatz, Galvin, and Gagne ©19995.11 Many-to-many Model
  • 12. Silberschatz, Galvin, and Gagne ©19995.12 Solaris 2 Threads
  • 13. Silberschatz, Galvin, and Gagne ©19995.13 Solaris Process
  • 14. Silberschatz, Galvin, and Gagne ©19995.14 Java Threads • Java Threads May be Created by: – Extending Thread class – Implementing the Runnable interface
  • 15. Silberschatz, Galvin, and Gagne ©19995.15 Extending the Thread Class class Worker1 extends Thread { public void run() { System.out.println(“I am a Worker Thread”); } }
  • 16. Silberschatz, Galvin, and Gagne ©19995.16 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”); } }
  • 17. Silberschatz, Galvin, and Gagne ©19995.17 The Runnable Interface public interface Runnable { public abstract void run(); }
  • 18. Silberschatz, Galvin, and Gagne ©19995.18 Implementing the Runnable Interface class Worker2 implements Runnable { public void run() { System.out.println(“I am a Worker Thread”); } }
  • 19. Silberschatz, Galvin, and Gagne ©19995.19 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”); } }
  • 20. Silberschatz, Galvin, and Gagne ©19995.20 Java Thread Management • 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.
  • 21. Silberschatz, Galvin, and Gagne ©19995.21 Java Thread States
  • 22. Silberschatz, Galvin, and Gagne ©19995.22 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(); } }
  • 23. Silberschatz, Galvin, and Gagne ©19995.23 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; }
  • 24. Silberschatz, Galvin, and Gagne ©19995.24 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; }