SlideShare a Scribd company logo
Developing Multithreaded Applications
Where multithreading can be useful… Non-blocking I/O The blocking read() method Is blocking nature of read desirable? Shouldn’t I/O be dealt in an asynchronous way? Ways to overcome : - I/O Multiplexing Pooling Alarms & Timers Independent Tasks Think of Parallel processing instead of linear. Parallelizable Algorithms Ex : parallelization of loops.
Process-Based Multitasking Processes in a Multitasking Environment
Thread-Based Multitasking Threads in a Multithreaded Environment
Ways of Creating a Thread : 2 Extend java.lang.Thread Implement  java.lang.Runnable java.lang.Object java.lang.Thread A user-defined class java.lang.Runnable A user-defined class
Extending java.lang.Thread class MyThread extends Thread { public void run()  // overriding run { // code that the thread will execute } } ------------------------------------------------ public static void main(String[] args) { Thread t = new MyThread(); t.start();  // creating a thread executing a task parallel to others // code the main thread will execute }
Implementing java.lang.Runnable class  MyThread implements Runnable { public void run()  // implementing run() { // code to be executed } } --------------------------------- public static void main(String[] args) { Thread t = new Thread(new MyThread(),”Thread2”); t.start(); // Let main thread do something else }
Life-Cycle of a Thread Intermediate States : suspend, waiting/blocking etc.
Let ur thread participate in the race : call start() A thread exists once it has been constructed, but at that point it is not executing any code. The thread is in a waiting state. Thread t = new MyThread(); Once start() is invoked, the thread is said to be alive. Check the state of the thread before & after invoking start(). use isAlive() Other threads can change the attributes of a waiting thread.
Terminating a Thread Normal termination of life-cycle. Abnormal or forced termination Once a thread is started, it call only one method i.e., run(). When it completes run, the thread is said to be terminated. What can cause abnormal termination of a thread? Can run( ) throw a checked exception? How can a thread be forced to terminate?
Pausing, Suspending & Resuming a thread A running thread can be told to pause or resume. main thread Thread t1 I must do the things first, I’m going to suspend t1. t1.suspend(); // main did its work t1.resume(); A running thread can pause its own execution for a specified time using  sleep()
Ad

More Related Content

What's hot (19)

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
AmbigaMurugesan
 
Thread
ThreadThread
Thread
Juhi Kumari
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
Arvind Krishnaa
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
M. Raihan
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Multi threading
Multi threadingMulti threading
Multi threading
gndu
 
Java threading
Java threadingJava threading
Java threading
Chinh Ngo Nguyen
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors Framework
Arun Mehra
 
Java thread
Java threadJava thread
Java thread
Arati Gadgil
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37
myrajendra
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
Appsterdam Milan
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
Hemo Chella
 
Multi threading
Multi threadingMulti threading
Multi threading
Mavoori Soshmitha
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
jehan1987
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
caswenson
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
M. Raihan
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Multi threading
Multi threadingMulti threading
Multi threading
gndu
 
Java Multithreading Using Executors Framework
Java Multithreading Using Executors FrameworkJava Multithreading Using Executors Framework
Java Multithreading Using Executors Framework
Arun Mehra
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37
myrajendra
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
jehan1987
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
caswenson
 

Similar to Developing Multithreaded Applications (20)

unit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptxunit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
unit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptxunit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
Rakesh Madugula
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
HKShab
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
ramyan49
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
Shipra Swati
 
Multi-Threading in Java power point presenetation
Multi-Threading in Java power point presenetationMulti-Threading in Java power point presenetation
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
Elizabeth alexander
 
Java Threads
Java ThreadsJava Threads
Java Threads
Hamid Ghorbani
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 
multithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptxmultithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
HemantSharma134028
 
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
JAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transactionJAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transaction
SaikiranBiradar3
 
multithreading.pptx
multithreading.pptxmultithreading.pptx
multithreading.pptx
Sravanibitragunta
 
topic_presentation_java_threads_example.ppt
topic_presentation_java_threads_example.ppttopic_presentation_java_threads_example.ppt
topic_presentation_java_threads_example.ppt
ShitalBahale2
 
java threads for easy learn and develop
java  threads for easy learn and developjava  threads for easy learn and develop
java threads for easy learn and develop
pavanrock859
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
junnubabu
 
unit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptxunit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
unit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptxunit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
HKShab
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
ramyan49
 
Multi-Threading in Java power point presenetation
Multi-Threading in Java power point presenetationMulti-Threading in Java power point presenetation
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
Elizabeth alexander
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 
multithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptxmultithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
JAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transactionJAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transaction
SaikiranBiradar3
 
topic_presentation_java_threads_example.ppt
topic_presentation_java_threads_example.ppttopic_presentation_java_threads_example.ppt
topic_presentation_java_threads_example.ppt
ShitalBahale2
 
java threads for easy learn and develop
java  threads for easy learn and developjava  threads for easy learn and develop
java threads for easy learn and develop
pavanrock859
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
junnubabu
 
Ad

More from Bharat17485 (12)

Channel Based Io
Channel Based IoChannel Based Io
Channel Based Io
Bharat17485
 
Core Java
Core JavaCore Java
Core Java
Bharat17485
 
Interfaces & Abstract Classes
Interfaces & Abstract ClassesInterfaces & Abstract Classes
Interfaces & Abstract Classes
Bharat17485
 
Enum
EnumEnum
Enum
Bharat17485
 
Exceptions & Its Handling
Exceptions & Its HandlingExceptions & Its Handling
Exceptions & Its Handling
Bharat17485
 
Jstl & El
Jstl & ElJstl & El
Jstl & El
Bharat17485
 
Primitive Wrappers
Primitive WrappersPrimitive Wrappers
Primitive Wrappers
Bharat17485
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
Bharat17485
 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input Output
Bharat17485
 
String Handling
String HandlingString Handling
String Handling
Bharat17485
 
Swing
SwingSwing
Swing
Bharat17485
 
Applying Generics
Applying GenericsApplying Generics
Applying Generics
Bharat17485
 
Channel Based Io
Channel Based IoChannel Based Io
Channel Based Io
Bharat17485
 
Interfaces & Abstract Classes
Interfaces & Abstract ClassesInterfaces & Abstract Classes
Interfaces & Abstract Classes
Bharat17485
 
Exceptions & Its Handling
Exceptions & Its HandlingExceptions & Its Handling
Exceptions & Its Handling
Bharat17485
 
Primitive Wrappers
Primitive WrappersPrimitive Wrappers
Primitive Wrappers
Bharat17485
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
Bharat17485
 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input Output
Bharat17485
 
Applying Generics
Applying GenericsApplying Generics
Applying Generics
Bharat17485
 
Ad

Recently uploaded (20)

On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Artificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptxArtificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptx
03ANMOLCHAURASIYA
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Artificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptxArtificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptx
03ANMOLCHAURASIYA
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 

Developing Multithreaded Applications

  • 2. Where multithreading can be useful… Non-blocking I/O The blocking read() method Is blocking nature of read desirable? Shouldn’t I/O be dealt in an asynchronous way? Ways to overcome : - I/O Multiplexing Pooling Alarms & Timers Independent Tasks Think of Parallel processing instead of linear. Parallelizable Algorithms Ex : parallelization of loops.
  • 3. Process-Based Multitasking Processes in a Multitasking Environment
  • 4. Thread-Based Multitasking Threads in a Multithreaded Environment
  • 5. Ways of Creating a Thread : 2 Extend java.lang.Thread Implement java.lang.Runnable java.lang.Object java.lang.Thread A user-defined class java.lang.Runnable A user-defined class
  • 6. Extending java.lang.Thread class MyThread extends Thread { public void run() // overriding run { // code that the thread will execute } } ------------------------------------------------ public static void main(String[] args) { Thread t = new MyThread(); t.start(); // creating a thread executing a task parallel to others // code the main thread will execute }
  • 7. Implementing java.lang.Runnable class MyThread implements Runnable { public void run() // implementing run() { // code to be executed } } --------------------------------- public static void main(String[] args) { Thread t = new Thread(new MyThread(),”Thread2”); t.start(); // Let main thread do something else }
  • 8. Life-Cycle of a Thread Intermediate States : suspend, waiting/blocking etc.
  • 9. Let ur thread participate in the race : call start() A thread exists once it has been constructed, but at that point it is not executing any code. The thread is in a waiting state. Thread t = new MyThread(); Once start() is invoked, the thread is said to be alive. Check the state of the thread before & after invoking start(). use isAlive() Other threads can change the attributes of a waiting thread.
  • 10. Terminating a Thread Normal termination of life-cycle. Abnormal or forced termination Once a thread is started, it call only one method i.e., run(). When it completes run, the thread is said to be terminated. What can cause abnormal termination of a thread? Can run( ) throw a checked exception? How can a thread be forced to terminate?
  • 11. Pausing, Suspending & Resuming a thread A running thread can be told to pause or resume. main thread Thread t1 I must do the things first, I’m going to suspend t1. t1.suspend(); // main did its work t1.resume(); A running thread can pause its own execution for a specified time using sleep()
  翻译: