SlideShare a Scribd company logo
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.
Java threads
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.

More Related Content

PPT
Threads And Synchronization in C#
PPTX
Threading in C#
PPTX
.NET: Thread Synchronization Constructs
PPTX
.NET Multithreading/Multitasking
PPTX
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
PPT
Threads c sharp
PDF
.Net Threading
PPTX
Multithreading in java
Threads And Synchronization in C#
Threading in C#
.NET: Thread Synchronization Constructs
.NET Multithreading/Multitasking
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Threads c sharp
.Net Threading
Multithreading in java

What's hot (16)

PDF
Introduction to Akka
PPTX
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
PPT
Chap2 2 1
PPTX
Java Multi Thead Programming
PPTX
Multi threading
PPTX
Introduction to Multithreading in Java
PPT
Java Threads
PPTX
Threading
PPT
Java Multithreading
PPTX
Java concurrency in practice
PPTX
Concurrency in c#
PPT
Android - Thread, Handler and AsyncTask
PPTX
Multi-Threading
PPT
Java And Multithreading
PDF
Java threads
PPTX
Concurrency with java
Introduction to Akka
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Chap2 2 1
Java Multi Thead Programming
Multi threading
Introduction to Multithreading in Java
Java Threads
Threading
Java Multithreading
Java concurrency in practice
Concurrency in c#
Android - Thread, Handler and AsyncTask
Multi-Threading
Java And Multithreading
Java threads
Concurrency with java
Ad

Similar to Java threads (20)

DOCX
Threadnotes
PDF
Class notes(week 9) on multithreading
PDF
Multithreading Introduction and Lifecyle of thread
PPTX
Lecture 23-24.pptx
PPTX
Multithreading.pptx
PPTX
Multithreading in java
PDF
java-thread
PPTX
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
PPT
Threads
PDF
Java Multithreading Using Executors Framework
PPT
web programming-Multithreading concept in Java.ppt
PPTX
Java Multithreading - how to create threads
PPTX
Multithreading in java
PPT
Java multithreading
PPTX
Object-Oriented-Prog_MultiThreading.pptx
PPTX
Multithreading
PPT
PPT
cs4240-multithreading.ppt presentation on multi threading
PPT
Basic of Multithreading in JAva
PPT
Md09 multithreading
Threadnotes
Class notes(week 9) on multithreading
Multithreading Introduction and Lifecyle of thread
Lecture 23-24.pptx
Multithreading.pptx
Multithreading in java
java-thread
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Threads
Java Multithreading Using Executors Framework
web programming-Multithreading concept in Java.ppt
Java Multithreading - how to create threads
Multithreading in java
Java multithreading
Object-Oriented-Prog_MultiThreading.pptx
Multithreading
cs4240-multithreading.ppt presentation on multi threading
Basic of Multithreading in JAva
Md09 multithreading
Ad

More from javaicon (6)

PDF
Java basic data types
PDF
Java Basic Syntax
PDF
What is java
PDF
Life cycle-of-a-thread
PDF
Processes and-threads
PDF
Java basic-syntax
Java basic data types
Java Basic Syntax
What is java
Life cycle-of-a-thread
Processes and-threads
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.
  • 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.