SlideShare a Scribd company logo
1 of 8
Home
Creation of Threads
• Java provides two ways for creating threads by
   • Extending Thread class
   • Implementing Runnable interface




                                                   2
Context Switch
• Switch from one thread to other
• Two ways of context switch
   • Thread can voluntarily relinquish control by
       • Explicitly yielding
       • Sleeping
       • Blocking on pending I/O
       • Context switch is done by selecting the highest priority thread
   • Pre-emption
       • Occurs when a high priority enters while executing a low
         priority thread



                                                                           3
Thread Priorities
• Thread priorities are simple integers
   • Ranging from 1 to 10
   • A relative measure
   • Higher the priority – brighter the chances of getting
     executed first




                                                             4
Synchronization
• As threads introduces asynchronous behavior in Java
  programs
   • Synchronization is needed when two or more
     threads work with a shared resource
   • ‘Monitor’ feature is used in Java to implement
     Synchronization
   • Monitors are used mutually exclusive




                                                        5
Synchronization contd..
• There is no special monitor class in Java
• Every object has its own implicit monitor
• Two ways to show synchronization
   • synchronized methods
   • synchronized statements




                                              6
Messaging

• Java threads communicate with each other through

  • notify() method

  • wait() method




                                                     7
Summary
In this class ,you have learnt
• Thread basics
       • Process
       • Definition of thread
       • Comparison of process and thread
       • Thread model of Java
       • Features of Java that implemented threads




                                                     8

More Related Content

Viewers also liked

Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsTushar B Kute
 
Basic Thread Knowledge
Basic Thread KnowledgeBasic Thread Knowledge
Basic Thread KnowledgeShipra Roy
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronizationcaswenson
 
Posix threads(asha)
Posix threads(asha)Posix threads(asha)
Posix threads(asha)Nagarajan
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
 
Ch5: Threads (Operating System)
Ch5: Threads (Operating System)Ch5: Threads (Operating System)
Ch5: Threads (Operating System)Ahmar Hashmi
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-GalvinSonali Chauhan
 

Viewers also liked (15)

Threads
ThreadsThreads
Threads
 
P-Threads
P-ThreadsP-Threads
P-Threads
 
Thread dumps
Thread dumpsThread dumps
Thread dumps
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix Threads
 
Pthread
PthreadPthread
Pthread
 
P threads
P threadsP threads
P threads
 
Basic Thread Knowledge
Basic Thread KnowledgeBasic Thread Knowledge
Basic Thread Knowledge
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
 
Posix threads(asha)
Posix threads(asha)Posix threads(asha)
Posix threads(asha)
 
Posix Threads
Posix ThreadsPosix Threads
Posix Threads
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Ch5: Threads (Operating System)
Ch5: Threads (Operating System)Ch5: Threads (Operating System)
Ch5: Threads (Operating System)
 
Threads
ThreadsThreads
Threads
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
 

Similar to Creation of threads (20)

U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
 
Thread.ppt
Thread.pptThread.ppt
Thread.ppt
 
Java threading
Java threadingJava threading
Java threading
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
 
Thread
ThreadThread
Thread
 
Thread
ThreadThread
Thread
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practice
 
Thread.ppt
Thread.pptThread.ppt
Thread.ppt
 
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
 
Concurrency in java
Concurrency in javaConcurrency in java
Concurrency in java
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Java Classroom Training
Java Classroom TrainingJava Classroom Training
Java Classroom Training
 
Java Online Training
Java Online TrainingJava Online Training
Java Online Training
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
15 threads
15 threads15 threads
15 threads
 
Java gc
Java gcJava gc
Java gc
 
Java Garbage Collection
Java Garbage CollectionJava Garbage Collection
Java Garbage Collection
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
vp.pdf
vp.pdfvp.pdf
vp.pdf
 

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Creation of threads

  • 2. Creation of Threads • Java provides two ways for creating threads by • Extending Thread class • Implementing Runnable interface 2
  • 3. Context Switch • Switch from one thread to other • Two ways of context switch • Thread can voluntarily relinquish control by • Explicitly yielding • Sleeping • Blocking on pending I/O • Context switch is done by selecting the highest priority thread • Pre-emption • Occurs when a high priority enters while executing a low priority thread 3
  • 4. Thread Priorities • Thread priorities are simple integers • Ranging from 1 to 10 • A relative measure • Higher the priority – brighter the chances of getting executed first 4
  • 5. Synchronization • As threads introduces asynchronous behavior in Java programs • Synchronization is needed when two or more threads work with a shared resource • ‘Monitor’ feature is used in Java to implement Synchronization • Monitors are used mutually exclusive 5
  • 6. Synchronization contd.. • There is no special monitor class in Java • Every object has its own implicit monitor • Two ways to show synchronization • synchronized methods • synchronized statements 6
  • 7. Messaging • Java threads communicate with each other through • notify() method • wait() method 7
  • 8. Summary In this class ,you have learnt • Thread basics • Process • Definition of thread • Comparison of process and thread • Thread model of Java • Features of Java that implemented threads 8