SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
Thread Objects
Each thread is associated with an instance
of the class Thread.
Defining and Starting a Thread
Three ways
● Provide a Runnable object.
● Subclass Thread.
● Using Executor Interface
public interface Runnable
The Runnable interface should be implemented by any class whose
instances are intended to be executed by a thread. The class must
define a method of no arguments called run.
void run( )
starting the thread causes the object's run method to be called in
that separately executing thread.
Hello from a thread!
Using Inner class
Hello from a thread!
Extending Thread class
The Thread class itself implements Runnable, though its run method
does nothing. An application can subclass Thread, providing its own
implementation of run method.
Hello from a thread!
Using Inner class
Hello from a thread!
Thread.start ( )
Runnable vs. Thread
● Runnable object, is more general, because the Runnable object can
subclass a class other than Thread.
● Sub-classing Thread is easier to use in simple applications or Testing
code , but is limited by the fact that your task class must be a
descendant of Thread
Thread scheduling
How each task will be run, including details of thread use, scheduling,
etc.
This code need to be seprated from the
instantiating thread class.
a way of decoupling task submission from the mechanics of
how each task will be run.
Task submission
The Executor will provide the mechanics of how Task will be executed.
java.util.concurrent.Executor
An object that executes submitted Runnable tasks.
The best approach is to separate the Runnable from the execution task
and it is compulsory for large-scale applications.
Pausing Execution with Sleep
Thread.sleep causes the current thread to suspend execution for a
specified period.
This is an efficient means of making processor time available to the other
threads of an application.
Quotes.java
prints one Quote,
pauses for 1 second, prints next Quote, ...
Interrupted Exception
sleep throws Interrupted Exception when another thread interrupts the
current thread while sleep is active.
Two overloaded versions of sleep are provided
● sleep time = millisecond
● sleep time = nanosecond
However, these sleep times are not guaranteed to be precise, because
they are limited by the facilities provided by the underlying OS.

Contenu connexe

Tendances

Introduction to Akka
Introduction to AkkaIntroduction to Akka
Introduction to AkkaKnoldus Inc.
 
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Sachintha Gunasena
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead ProgrammingNishant Mevawala
 
Multi threading
Multi threadingMulti threading
Multi threadinggndu
 
Introduction to Multithreading in Java
Introduction to Multithreading in JavaIntroduction to Multithreading in Java
Introduction to Multithreading in JavaRakesh Mittal
 
Java Multithreading
Java MultithreadingJava Multithreading
Java MultithreadingRajkattamuri
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practiceMikalai Alimenkou
 
Android - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTaskAndroid - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTaskHoang Ngo
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaHoang Nguyen
 

Tendances (16)

Introduction to Akka
Introduction to AkkaIntroduction to Akka
Introduction to Akka
 
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Introduction to Multithreading in Java
Introduction to Multithreading in JavaIntroduction to Multithreading in Java
Introduction to Multithreading in Java
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Threading
ThreadingThreading
Threading
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practice
 
Concurrency in c#
Concurrency in c#Concurrency in c#
Concurrency in c#
 
Android - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTaskAndroid - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTask
 
Multi-Threading
Multi-ThreadingMulti-Threading
Multi-Threading
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java threads
Java threadsJava threads
Java threads
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 

Similaire à Java threads

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAvasuraj pandey
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and ConcurrencyRajesh Ananda Kumar
 
Lecture 23-24.pptx
Lecture 23-24.pptxLecture 23-24.pptx
Lecture 23-24.pptxtalha ijaz
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptxTREXSHyNE
 
Lec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingLec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingBadar Waseer
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptxnimbalkarvikram966
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in javaElizabeth alexander
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in javaDucat India
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
Unit No 4 Exception Handling and Multithreading.pptx
Unit No 4 Exception Handling and Multithreading.pptxUnit No 4 Exception Handling and Multithreading.pptx
Unit No 4 Exception Handling and Multithreading.pptxDrYogeshDeshmukh1
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptxramyan49
 

Similaire à Java threads (20)

Threadnotes
ThreadnotesThreadnotes
Threadnotes
 
Slide 7 Thread-1.pptx
Slide 7 Thread-1.pptxSlide 7 Thread-1.pptx
Slide 7 Thread-1.pptx
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Lecture 23-24.pptx
Lecture 23-24.pptxLecture 23-24.pptx
Lecture 23-24.pptx
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptx
 
Lec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingLec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented Programming
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Unit No 4 Exception Handling and Multithreading.pptx
Unit No 4 Exception Handling and Multithreading.pptxUnit No 4 Exception Handling and Multithreading.pptx
Unit No 4 Exception Handling and Multithreading.pptx
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
 
Java threading
Java threadingJava threading
Java threading
 

Plus de javaicon

Java basic data types
Java basic data typesJava basic data types
Java basic data typesjavaicon
 
Java Basic Syntax
Java Basic SyntaxJava Basic Syntax
Java Basic Syntaxjavaicon
 
What is java
What is javaWhat is java
What is javajavaicon
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-threadjavaicon
 
Processes and-threads
Processes and-threadsProcesses and-threads
Processes and-threadsjavaicon
 
Java basic-syntax
Java basic-syntaxJava basic-syntax
Java basic-syntaxjavaicon
 

Plus de javaicon (6)

Java basic data types
Java basic data typesJava basic data types
Java basic data types
 
Java Basic Syntax
Java Basic SyntaxJava Basic Syntax
Java Basic Syntax
 
What is java
What is javaWhat is java
What is java
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
Processes and-threads
Processes and-threadsProcesses and-threads
Processes and-threads
 
Java basic-syntax
Java basic-syntaxJava basic-syntax
Java basic-syntax
 

Java threads

  • 1. Thread Objects Each thread is associated with an instance of the class Thread.
  • 2. Defining and Starting a Thread Three ways ● Provide a Runnable object. ● Subclass Thread. ● Using Executor Interface
  • 3. public interface Runnable The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run. void run( ) starting the thread causes the object's run method to be called in that separately executing thread.
  • 4. Hello from a thread!
  • 5. Using Inner class Hello from a thread!
  • 6. Extending Thread class The Thread class itself implements Runnable, though its run method does nothing. An application can subclass Thread, providing its own implementation of run method.
  • 7. Hello from a thread!
  • 8. Using Inner class Hello from a thread!
  • 10. Runnable vs. Thread ● Runnable object, is more general, because the Runnable object can subclass a class other than Thread. ● Sub-classing Thread is easier to use in simple applications or Testing code , but is limited by the fact that your task class must be a descendant of Thread
  • 11. Thread scheduling How each task will be run, including details of thread use, scheduling, etc. This code need to be seprated from the instantiating thread class.
  • 12. a way of decoupling task submission from the mechanics of how each task will be run. Task submission The Executor will provide the mechanics of how Task will be executed.
  • 13. java.util.concurrent.Executor An object that executes submitted Runnable tasks.
  • 14.
  • 15. The best approach is to separate the Runnable from the execution task and it is compulsory for large-scale applications.
  • 16. Pausing Execution with Sleep Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application.
  • 17. Quotes.java prints one Quote, pauses for 1 second, prints next Quote, ...
  • 18. Interrupted Exception sleep throws Interrupted Exception when another thread interrupts the current thread while sleep is active.
  • 19. Two overloaded versions of sleep are provided ● sleep time = millisecond ● sleep time = nanosecond However, these sleep times are not guaranteed to be precise, because they are limited by the facilities provided by the underlying OS.