SlideShare a Scribd company logo
1 of 9
CS3135/CS2135
Object Oriented Programming (Java)
BSCS-3 / MCS-3
Lecture # 20
Multi Threading
Concurrency
Thread
• A portion of a program that can run independently of and concurrently with other portions of the
program.
• Multithreading is the ability to do multiple things at once within the same application.
• Individual and separate unit of execution that is part of a process.
• Multiple threads can work together to accomplish a common goal
Multi Threading
• It is easy to confuse multithreading with multitasking or multiprogramming, which are somewhat
different ideas.
• Multithreading is the ability of a program or an operating system process to manage its use by more
than one user at a time and to even manage multiple requests by the same user without having
multiple copies of the program running in the computer.
• Java includes built-in support for threading.
Instructor: Tanzila Kehkashan
2
MultiThreading
Advantages
• Easier to program. (1 thread per task)
• Can provide better performance
• Thread only runs when needed
• No polling to decide what to do
• Multiple threads can share resources
• Utilize multiple processors if available
Disadvantages
• Multiple threads can lead to deadlock
• Overhead of switching between threads
Instructor: Tanzila Kehkashan
3
Thread States: Life Cycle of Thread
New
• Life cycle started. Just created but not started execution.
Runnable
• Created, started, and executing.
Waiting
• Sometimes a runnable thread transitions to waiting state while it
waits for another thread to perform a task. A waiting thread
transitions back to the runnable state only when another thread
notifies it to continue executing.
• Cannot use processor.
Timed Waiting
• Also called Sleep state.
• A runnable thread can enter timed waiting state for specified interval of time (sleep interval) . It transitions back to
runnable state when that time interval expires.
• Cannot use processor.
Blocked
• In block state, a runnable thread is unable to run because it is waiting for some event to occur. (for example I/O)
Dead / Terminated
• Runnable thread has successfully completed its task or otherwise terminates perhaps due to error.
Instructor: Tanzila Kehkashan
4
Multi-Threading
• There are two ways to create a new thread of execution.
• One is to declare a class to be a subclass of Thread.
• This subclass should override the run method of class Thread.
• An instance of the subclass can then be allocated and started.
• For example;
class myClass extends Thread
{
long minPrime;
myClass(long minPrime)
{
this.minPrime = minPrime;
}
public void run()
{
// compute primes larger than minPrime
. . .
}
}
myClass p = new myClass(143);
p.start();
Instructor: Tanzila Kehkashan
5
Multi-Threading
• The other way is to declare a class that implements the Runnable interface.
• That class then implements the run method.
• An instance of the class can then be allocated, passed as an argument when creating Thread, and
started.
• For example; class myClass implements Runnable
{
long minPrime;
PrimeRun(long minPrime)
{
this.minPrime = minPrime;
}
public void run()
{
// compute primes larger than minPrime
. . .
}
}
myClass obj = new myClass(143);
Thread t=new Thread(obj);
t.start(); //new Thread(obj).start();
Instructor: Tanzila Kehkashan
6
Multi-Threading
java.lang.Thread
• Thread()
• Thread(String) //name of thread
• long getId() //returns ID of thread
• String getName() //name of thread
• void run()
• static void sleep(long) //milliseconds //temporarily cease execution
• void start() //calls run method
Instructor: Tanzila Kehkashan
7
Instructor: Tanzila Kehkashan
8
Instructor: Tanzila Kehkashan
9

More Related Content

What's hot

Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in javaAhsan Raja
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdmHarshal Misalkar
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using javaNarayan Sau
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threadingAntonio Cesarano
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programmingAnyapuPranav
 
Protection and security of operating system
Protection and security of operating systemProtection and security of operating system
Protection and security of operating systemAbdullah Khosa
 
Java Multithreading
Java MultithreadingJava Multithreading
Java MultithreadingRajkattamuri
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAsivasundari6
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
Java multi threading
Java multi threadingJava multi threading
Java multi threadingRaja Sekhar
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 

What's hot (20)

Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threading
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
 
Threads .ppt
Threads .pptThreads .ppt
Threads .ppt
 
Protection and security of operating system
Protection and security of operating systemProtection and security of operating system
Protection and security of operating system
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Os Threads
Os ThreadsOs Threads
Os Threads
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 

Similar to CS3135 Multi-Threading Lecture

Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptxssuserfcae42
 
Lec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingLec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingBadar Waseer
 
multithreadingppt.pptx
multithreadingppt.pptxmultithreadingppt.pptx
multithreadingppt.pptxFardeenAzhar
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptxTREXSHyNE
 
Multi threading
Multi threadingMulti threading
Multi threadinggndu
 
Multithreading in Scala
Multithreading in Scala Multithreading in Scala
Multithreading in Scala Knoldus Inc.
 
Mulitthread
MulitthreadMulitthread
MulitthreadDeepaR42
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptxsandhyakiran10
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in javaElizabeth alexander
 
Java Threads: Lightweight Processes
Java Threads: Lightweight ProcessesJava Threads: Lightweight Processes
Java Threads: Lightweight ProcessesIsuru Perera
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreadingjehan1987
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptxmadan r
 

Similar to CS3135 Multi-Threading Lecture (20)

Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Lec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingLec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented Programming
 
multithreadingppt.pptx
multithreadingppt.pptxmultithreadingppt.pptx
multithreadingppt.pptx
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptx
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in Scala
Multithreading in Scala Multithreading in Scala
Multithreading in Scala
 
Mulitthread
MulitthreadMulitthread
Mulitthread
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java Threads: Lightweight Processes
Java Threads: Lightweight ProcessesJava Threads: Lightweight Processes
Java Threads: Lightweight Processes
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
Thread
ThreadThread
Thread
 

More from Tanzila Kehkashan

OOP Lecture 25-Network Programming-Part2.pptx
OOP Lecture 25-Network Programming-Part2.pptxOOP Lecture 25-Network Programming-Part2.pptx
OOP Lecture 25-Network Programming-Part2.pptxTanzila Kehkashan
 
OOP Lecture 24-Network Programming-Part1.pptx
OOP Lecture 24-Network Programming-Part1.pptxOOP Lecture 24-Network Programming-Part1.pptx
OOP Lecture 24-Network Programming-Part1.pptxTanzila Kehkashan
 
OOP Lecture 23-JAR Files.pptx
OOP Lecture 23-JAR Files.pptxOOP Lecture 23-JAR Files.pptx
OOP Lecture 23-JAR Files.pptxTanzila Kehkashan
 
OOP Lecture 21-Graphics, Audio.pptx
OOP Lecture 21-Graphics, Audio.pptxOOP Lecture 21-Graphics, Audio.pptx
OOP Lecture 21-Graphics, Audio.pptxTanzila Kehkashan
 
OOP Lecture 19-JMenuBar.pptx
OOP Lecture 19-JMenuBar.pptxOOP Lecture 19-JMenuBar.pptx
OOP Lecture 19-JMenuBar.pptxTanzila Kehkashan
 
OOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptxOOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptxTanzila Kehkashan
 
OOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptxOOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptxTanzila Kehkashan
 
OOP Lecture 16-Math,Timer.pptx
OOP Lecture 16-Math,Timer.pptxOOP Lecture 16-Math,Timer.pptx
OOP Lecture 16-Math,Timer.pptxTanzila Kehkashan
 
OOP Lecture 15-FileHandling,JFileChooser.pptx
OOP Lecture 15-FileHandling,JFileChooser.pptxOOP Lecture 15-FileHandling,JFileChooser.pptx
OOP Lecture 15-FileHandling,JFileChooser.pptxTanzila Kehkashan
 
OOP Lecture 14-ExceptionHandling.pptx
OOP Lecture 14-ExceptionHandling.pptxOOP Lecture 14-ExceptionHandling.pptx
OOP Lecture 14-ExceptionHandling.pptxTanzila Kehkashan
 
OOP Lecture 13-Color,Font,ImageIcon.pptx
OOP Lecture 13-Color,Font,ImageIcon.pptxOOP Lecture 13-Color,Font,ImageIcon.pptx
OOP Lecture 13-Color,Font,ImageIcon.pptxTanzila Kehkashan
 
OOP Lecture 12-EventHandling2.pptx
OOP Lecture 12-EventHandling2.pptxOOP Lecture 12-EventHandling2.pptx
OOP Lecture 12-EventHandling2.pptxTanzila Kehkashan
 
OOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxOOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxTanzila Kehkashan
 
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptxOOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptxTanzila Kehkashan
 
OOP Lecture 9-JComboBox,JList,JPanel.pptx
OOP Lecture 9-JComboBox,JList,JPanel.pptxOOP Lecture 9-JComboBox,JList,JPanel.pptx
OOP Lecture 9-JComboBox,JList,JPanel.pptxTanzila Kehkashan
 
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptx
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptxOOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptx
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptxTanzila Kehkashan
 
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptx
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptxOOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptx
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptxTanzila Kehkashan
 
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptx
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptxOOP Lecture 6-JFrame,JScrollPane,JLabel.pptx
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptxTanzila Kehkashan
 
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptx
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptxOOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptx
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptxTanzila Kehkashan
 

More from Tanzila Kehkashan (20)

OOP Lecture 25-Network Programming-Part2.pptx
OOP Lecture 25-Network Programming-Part2.pptxOOP Lecture 25-Network Programming-Part2.pptx
OOP Lecture 25-Network Programming-Part2.pptx
 
OOP Lecture 24-Network Programming-Part1.pptx
OOP Lecture 24-Network Programming-Part1.pptxOOP Lecture 24-Network Programming-Part1.pptx
OOP Lecture 24-Network Programming-Part1.pptx
 
OOP Lecture 23-JAR Files.pptx
OOP Lecture 23-JAR Files.pptxOOP Lecture 23-JAR Files.pptx
OOP Lecture 23-JAR Files.pptx
 
OOP Lecture 22-JApplet.pptx
OOP Lecture 22-JApplet.pptxOOP Lecture 22-JApplet.pptx
OOP Lecture 22-JApplet.pptx
 
OOP Lecture 21-Graphics, Audio.pptx
OOP Lecture 21-Graphics, Audio.pptxOOP Lecture 21-Graphics, Audio.pptx
OOP Lecture 21-Graphics, Audio.pptx
 
OOP Lecture 19-JMenuBar.pptx
OOP Lecture 19-JMenuBar.pptxOOP Lecture 19-JMenuBar.pptx
OOP Lecture 19-JMenuBar.pptx
 
OOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptxOOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptx
 
OOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptxOOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptx
 
OOP Lecture 16-Math,Timer.pptx
OOP Lecture 16-Math,Timer.pptxOOP Lecture 16-Math,Timer.pptx
OOP Lecture 16-Math,Timer.pptx
 
OOP Lecture 15-FileHandling,JFileChooser.pptx
OOP Lecture 15-FileHandling,JFileChooser.pptxOOP Lecture 15-FileHandling,JFileChooser.pptx
OOP Lecture 15-FileHandling,JFileChooser.pptx
 
OOP Lecture 14-ExceptionHandling.pptx
OOP Lecture 14-ExceptionHandling.pptxOOP Lecture 14-ExceptionHandling.pptx
OOP Lecture 14-ExceptionHandling.pptx
 
OOP Lecture 13-Color,Font,ImageIcon.pptx
OOP Lecture 13-Color,Font,ImageIcon.pptxOOP Lecture 13-Color,Font,ImageIcon.pptx
OOP Lecture 13-Color,Font,ImageIcon.pptx
 
OOP Lecture 12-EventHandling2.pptx
OOP Lecture 12-EventHandling2.pptxOOP Lecture 12-EventHandling2.pptx
OOP Lecture 12-EventHandling2.pptx
 
OOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxOOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptx
 
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptxOOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
 
OOP Lecture 9-JComboBox,JList,JPanel.pptx
OOP Lecture 9-JComboBox,JList,JPanel.pptxOOP Lecture 9-JComboBox,JList,JPanel.pptx
OOP Lecture 9-JComboBox,JList,JPanel.pptx
 
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptx
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptxOOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptx
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptx
 
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptx
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptxOOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptx
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptx
 
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptx
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptxOOP Lecture 6-JFrame,JScrollPane,JLabel.pptx
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptx
 
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptx
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptxOOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptx
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptx
 

Recently uploaded

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 

Recently uploaded (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 

CS3135 Multi-Threading Lecture

  • 1. CS3135/CS2135 Object Oriented Programming (Java) BSCS-3 / MCS-3 Lecture # 20 Multi Threading
  • 2. Concurrency Thread • A portion of a program that can run independently of and concurrently with other portions of the program. • Multithreading is the ability to do multiple things at once within the same application. • Individual and separate unit of execution that is part of a process. • Multiple threads can work together to accomplish a common goal Multi Threading • It is easy to confuse multithreading with multitasking or multiprogramming, which are somewhat different ideas. • Multithreading is the ability of a program or an operating system process to manage its use by more than one user at a time and to even manage multiple requests by the same user without having multiple copies of the program running in the computer. • Java includes built-in support for threading. Instructor: Tanzila Kehkashan 2
  • 3. MultiThreading Advantages • Easier to program. (1 thread per task) • Can provide better performance • Thread only runs when needed • No polling to decide what to do • Multiple threads can share resources • Utilize multiple processors if available Disadvantages • Multiple threads can lead to deadlock • Overhead of switching between threads Instructor: Tanzila Kehkashan 3
  • 4. Thread States: Life Cycle of Thread New • Life cycle started. Just created but not started execution. Runnable • Created, started, and executing. Waiting • Sometimes a runnable thread transitions to waiting state while it waits for another thread to perform a task. A waiting thread transitions back to the runnable state only when another thread notifies it to continue executing. • Cannot use processor. Timed Waiting • Also called Sleep state. • A runnable thread can enter timed waiting state for specified interval of time (sleep interval) . It transitions back to runnable state when that time interval expires. • Cannot use processor. Blocked • In block state, a runnable thread is unable to run because it is waiting for some event to occur. (for example I/O) Dead / Terminated • Runnable thread has successfully completed its task or otherwise terminates perhaps due to error. Instructor: Tanzila Kehkashan 4
  • 5. Multi-Threading • There are two ways to create a new thread of execution. • One is to declare a class to be a subclass of Thread. • This subclass should override the run method of class Thread. • An instance of the subclass can then be allocated and started. • For example; class myClass extends Thread { long minPrime; myClass(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } } myClass p = new myClass(143); p.start(); Instructor: Tanzila Kehkashan 5
  • 6. Multi-Threading • The other way is to declare a class that implements the Runnable interface. • That class then implements the run method. • An instance of the class can then be allocated, passed as an argument when creating Thread, and started. • For example; class myClass implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } } myClass obj = new myClass(143); Thread t=new Thread(obj); t.start(); //new Thread(obj).start(); Instructor: Tanzila Kehkashan 6
  • 7. Multi-Threading java.lang.Thread • Thread() • Thread(String) //name of thread • long getId() //returns ID of thread • String getName() //name of thread • void run() • static void sleep(long) //milliseconds //temporarily cease execution • void start() //calls run method Instructor: Tanzila Kehkashan 7