SlideShare a Scribd company logo
By
B.SAI KIRAN
227R1A7374
TOPIC INCLUDES:
 Introduction to Thread
 Creation of Thread
 Life cycle of Thread
 Stopping and Blocking a Thread
 Using Thread Methods
MULTITHREADING
• Multithreading in java is a process of
executing multiple processes simultaneously
• A program is divided into two or more
subprograms, which can be implemented at
the same time in parallel.
• Multiprocessing and multithreading, both
are used to achieve multitasking.
• Java Multithreading is mostly used in games,
animation etc.
MULTITHREADINGContd.
CREATING THREAD
• Threads are implemented in the form of objects.
• The run() and start() are two inbuilt methods
which helps to thread implementation
• The run() method is the heart and soul of any
thread
– It makes up the entire body of a thread
• The run() method can be initiating with the help
of start() method.
CREATINGTHREAD Contd.
CREATING THREAD
1. By extending Thread class
2. By implementing Runnable
interface
CREATINGTHREAD
1. By Extending Thread class
class Multi extends Thread // Extending thread class
{
public void run() // run() method declared
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
Multi t1=new Multi(); //object initiated
t1.start(); // run() method called through start()
}
}
Output: thread is running…
CREATING THREAD
2. By implementing Runnable interface
 Define a class that implements Runnable
interface.
 The Runnable interface has only one method,
run(), that is to be defined in the method with the
code to be executed by the thread.
CREATINGTHREAD
2. By implementing Runnable interface
class Multi3 implements Runnable // Implementing Runnable interface
{
public void run()
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
Multi3 m1=new Multi3(); // object initiated for class
Thread t1 =new Thread(m1); // object initiated for thread
t1.start();
} }
Output: thread is running…
LIFE cycle of a thread
• During the life time of a thread, there are
many states it can enter.
• They include:
1. Newborn state
2. Runnable state
3. Running state
4. Blocked state
5. Dead state
LIFE cycle of a thread contd.
Thread methods
• Thread is a class found in java.lang package.
Method Signature Description
String getName() Retrieves the name of running thread in the current
context in String format
void start()
This method will start a new thread of execution by
calling run() method of Thread/runnable object.
void run() This method is the entry point of the thread. Execution of
thread starts from this method.
void sleep(int sleeptime)
This method suspend the thread for mentioned time
duration in argument (sleeptime in ms)
void yield()
By invoking this method the current thread pause its
execution temporarily and allow other threads to
execute.
void join()
This method used to queue up a thread in execution.
Once called on thread, current thread will wait till calling
thread completes its execution
boolean isAlive() This method will check if thread is alive or dead
Stopping and blocking
Stopping a thread:
• To stop a thread from running further, we may do
so by calling its stop() method.
• This causes a thread to stop immediately and
move it to its dead state.
• It forces the thread to stop abruptly before its
completion
• It causes premature death.
• To stop a thread we use the following syntax:
thread.stop();
Stopping and blocking
Blocking a Thread:
• A thread can also be temporarily suspended or blocked from
entering into the runnable and subsequently running state,
1. sleep(t) // blocked for ‘t’ milliseconds
2. suspend() // blocked until resume() method is
invoked
3. wait() // blocked until notify () is invoked
THANK YOU…

More Related Content

PPTX
Multithreadingppt.pptx
PPTX
unit3multithreadingppt-copy-180122162204.pptx
PPTX
unit3 Exception Handling multithreadingppt.pptx
PPTX
MULTI THREADING IN JAVA
PPTX
Multithreading.pptx
PPTX
econtent thread in java.pptx
PPT
Chap2 2 1
PPTX
Concept of Java Multithreading-Partially.pptx
Multithreadingppt.pptx
unit3multithreadingppt-copy-180122162204.pptx
unit3 Exception Handling multithreadingppt.pptx
MULTI THREADING IN JAVA
Multithreading.pptx
econtent thread in java.pptx
Chap2 2 1
Concept of Java Multithreading-Partially.pptx

Similar to JAVA THEORY PPT.pptx on based up on the transaction (20)

PDF
Java thread life cycle
PDF
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
PPTX
Multithreading in java
PDF
Java unit 12
PPT
Basic of Multithreading in JAva
PDF
Unit-3 MULTITHREADING-2.pdf
PPTX
OOPS object oriented programming UNIT-4.pptx
PPTX
multithreading,thread and processinjava-210302183809.pptx
PPTX
Multithreading in java
PPTX
Multithreading in Java Object Oriented Programming language
PDF
Multithreading Introduction and Lifecyle of thread
PPTX
U4 JAVA.pptx
PDF
Java threading
PPTX
Threading concepts
PPTX
Threads in Java
PPTX
MULTITHREADING IN JAVAMULTITHREADING IN -1.pptx
PPTX
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
PPT
web programming-Multithreading concept in Java.ppt
PPT
Java And Multithreading
PPTX
Multi threading
Java thread life cycle
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
Multithreading in java
Java unit 12
Basic of Multithreading in JAva
Unit-3 MULTITHREADING-2.pdf
OOPS object oriented programming UNIT-4.pptx
multithreading,thread and processinjava-210302183809.pptx
Multithreading in java
Multithreading in Java Object Oriented Programming language
Multithreading Introduction and Lifecyle of thread
U4 JAVA.pptx
Java threading
Threading concepts
Threads in Java
MULTITHREADING IN JAVAMULTITHREADING IN -1.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
web programming-Multithreading concept in Java.ppt
Java And Multithreading
Multi threading
Ad

Recently uploaded (20)

PPTX
mahatma gandhi bus terminal in india Case Study.pptx
PDF
Urban Design Final Project-Context
DOCX
The story of the first moon landing.docx
PDF
SEVA- Fashion designing-Presentation.pdf
PPTX
BSCS lesson 3.pptxnbbjbb mnbkjbkbbkbbkjb
PDF
The Advantages of Working With a Design-Build Studio
PPTX
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
DOCX
actividad 20% informatica microsoft project
PPT
UNIT I- Yarn, types, explanation, process
PPTX
DOC-20250430-WA0014._20250714_235747_0000.pptx
PPT
EGWHermeneuticsffgggggggggggggggggggggggggggggggg.ppt
PDF
YOW2022-BNE-MinimalViableArchitecture.pdf
PPTX
CLASS_11_BUSINESS_STUDIES_PPT_CHAPTER_1_Business_Trade_Commerce.pptx
PPTX
ANATOMY OF ANTERIOR CHAMBER ANGLE AND GONIOSCOPY.pptx
PPTX
An introduction to AI in research and reference management
PDF
BRANDBOOK-Presidential Award Scheme-Kenya-2023
PPTX
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
PPTX
Wisp Textiles: Where Comfort Meets Everyday Style
PDF
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
PPT
Machine printing techniques and plangi dyeing
mahatma gandhi bus terminal in india Case Study.pptx
Urban Design Final Project-Context
The story of the first moon landing.docx
SEVA- Fashion designing-Presentation.pdf
BSCS lesson 3.pptxnbbjbb mnbkjbkbbkbbkjb
The Advantages of Working With a Design-Build Studio
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
actividad 20% informatica microsoft project
UNIT I- Yarn, types, explanation, process
DOC-20250430-WA0014._20250714_235747_0000.pptx
EGWHermeneuticsffgggggggggggggggggggggggggggggggg.ppt
YOW2022-BNE-MinimalViableArchitecture.pdf
CLASS_11_BUSINESS_STUDIES_PPT_CHAPTER_1_Business_Trade_Commerce.pptx
ANATOMY OF ANTERIOR CHAMBER ANGLE AND GONIOSCOPY.pptx
An introduction to AI in research and reference management
BRANDBOOK-Presidential Award Scheme-Kenya-2023
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
Wisp Textiles: Where Comfort Meets Everyday Style
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
Machine printing techniques and plangi dyeing
Ad

JAVA THEORY PPT.pptx on based up on the transaction

  • 2. TOPIC INCLUDES:  Introduction to Thread  Creation of Thread  Life cycle of Thread  Stopping and Blocking a Thread  Using Thread Methods
  • 3. MULTITHREADING • Multithreading in java is a process of executing multiple processes simultaneously • A program is divided into two or more subprograms, which can be implemented at the same time in parallel. • Multiprocessing and multithreading, both are used to achieve multitasking. • Java Multithreading is mostly used in games, animation etc.
  • 5. CREATING THREAD • Threads are implemented in the form of objects. • The run() and start() are two inbuilt methods which helps to thread implementation • The run() method is the heart and soul of any thread – It makes up the entire body of a thread • The run() method can be initiating with the help of start() method.
  • 6. CREATINGTHREAD Contd. CREATING THREAD 1. By extending Thread class 2. By implementing Runnable interface
  • 7. CREATINGTHREAD 1. By Extending Thread class class Multi extends Thread // Extending thread class { public void run() // run() method declared { System.out.println("thread is running..."); } public static void main(String args[]) { Multi t1=new Multi(); //object initiated t1.start(); // run() method called through start() } } Output: thread is running…
  • 8. CREATING THREAD 2. By implementing Runnable interface  Define a class that implements Runnable interface.  The Runnable interface has only one method, run(), that is to be defined in the method with the code to be executed by the thread.
  • 9. CREATINGTHREAD 2. By implementing Runnable interface class Multi3 implements Runnable // Implementing Runnable interface { public void run() { System.out.println("thread is running..."); } public static void main(String args[]) { Multi3 m1=new Multi3(); // object initiated for class Thread t1 =new Thread(m1); // object initiated for thread t1.start(); } } Output: thread is running…
  • 10. LIFE cycle of a thread • During the life time of a thread, there are many states it can enter. • They include: 1. Newborn state 2. Runnable state 3. Running state 4. Blocked state 5. Dead state
  • 11. LIFE cycle of a thread contd.
  • 12. Thread methods • Thread is a class found in java.lang package. Method Signature Description String getName() Retrieves the name of running thread in the current context in String format void start() This method will start a new thread of execution by calling run() method of Thread/runnable object. void run() This method is the entry point of the thread. Execution of thread starts from this method. void sleep(int sleeptime) This method suspend the thread for mentioned time duration in argument (sleeptime in ms) void yield() By invoking this method the current thread pause its execution temporarily and allow other threads to execute. void join() This method used to queue up a thread in execution. Once called on thread, current thread will wait till calling thread completes its execution boolean isAlive() This method will check if thread is alive or dead
  • 13. Stopping and blocking Stopping a thread: • To stop a thread from running further, we may do so by calling its stop() method. • This causes a thread to stop immediately and move it to its dead state. • It forces the thread to stop abruptly before its completion • It causes premature death. • To stop a thread we use the following syntax: thread.stop();
  • 14. Stopping and blocking Blocking a Thread: • A thread can also be temporarily suspended or blocked from entering into the runnable and subsequently running state, 1. sleep(t) // blocked for ‘t’ milliseconds 2. suspend() // blocked until resume() method is invoked 3. wait() // blocked until notify () is invoked