SlideShare a Scribd company logo
1 of 10
Download to read offline
Multithreading
โ— A technique by which a single set of code can
be used by several processors at different
stages of execution.
Process and Threads
โ— A process is a thread in execution.
โ— A process may be divided ninto number of
independent units known as threads.
โ— A thread is a dispatchable unit of work.
โ— Threads are light-weight processes within a
process .
โ— A process is a collection of one or more
threads and associated system resources
Difference between Process and
Threads
โ— Process can be
divided into multiple
threads
โ— Each process has its
own memory space
โ— It is difficult to create
a process
โ— Threads cannot be
sub divided.
โ— Threads of the same
process share a
common memory
space
โ— It is easy to create a
thread.
Multitasking
โ— Multitasking is a method where multiple tasks
are performed during the same period of time
โ— They are executed concurrently instead of
sequentially
โ— The tasks share common processing
resources, such as a CPU and main memory.
Difference between multitasking and
multithreading
โ— An ability to run
several programs
simultaneously
potentially by using
several processors or
by time sharing the
resources available.
โ— An ability to run
serveral processes of
a single program
simultaneously
potentially using
several processors or
by time sharing the
resources available.
Life cycle of Thread
Contd'
โ—
NEW- A thread that is just instantiated is in new state. When a start()
method is invoked, the thread moves to the ready state from which it is
automatically moved to runnable state by the thread scheduler.
โ— RUNNABLE (ready running)-A thread executing in the JVM is in running
state.
โ—
BLOCKED - A thread that is blocked waiting for a monitor lock is in this
state. This can also occur when a thread performs an I/O operation and
moves to next (runnable) state.
โ— WAITING - A thread that is waiting indefinitely for another thread to
perform a particular action is in this state.
โ—
TIMED_WAITING - A thread that is waiting for another thread to perform
an action for up to a specified waiting time is in this state.
โ— TERMINATED - A thread that has exited is in this state.
Thread Creation
Threads can be created using two ways
โ— By implementing the Runnable class
โ— By extending the Thread class
โ— start() method is used to start the thread
โ— run() method is executed after calling the start()
โ— run() can contain the code you wish to perform
using thread.
Some of methods used in Threads
โ— start()- to begin the execution of thread
โ— setName()- to set the name for a thread
โ— getName()- to get the name of a thread
โ— Sleep() - to make the thread wait for specified
amount of time.
โ— setPriority()- to set the priority of the threads
โ— getPriority()- to get the priority of threads
Thread Creation Example
public class Threadlearning extends Thread
{
public void run()
{
System.out.println("i am thread ");
}
public static void main(String[] args)
{
Threadlearning t = new Threadlearning();
t.start();
}
}
public class Thread2way implements
Runnable
{
public void run()
{
System.out.println("i am in thread");
}
public static void main(String args[])
{
(new Thread(new Thread2way())).start();
}
}

More Related Content

What's hot

Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
ย 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
ย 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
ย 
Java interfaces
Java interfacesJava interfaces
Java interfacesRaja Sekhar
ย 
Finalize() method
Finalize() methodFinalize() method
Finalize() methodJadavsejal
ย 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
ย 
Java variable types
Java variable typesJava variable types
Java variable typesSoba Arjun
ย 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan Samoon
ย 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
ย 
Wrapper class
Wrapper classWrapper class
Wrapper classkamal kotecha
ย 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
ย 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
ย 

What's hot (20)

Java threads
Java threadsJava threads
Java threads
ย 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
ย 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
ย 
Java Streams
Java StreamsJava Streams
Java Streams
ย 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
ย 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
ย 
Interface in java
Interface in javaInterface in java
Interface in java
ย 
Java interfaces
Java interfacesJava interfaces
Java interfaces
ย 
Method overloading
Method overloadingMethod overloading
Method overloading
ย 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
ย 
Finalize() method
Finalize() methodFinalize() method
Finalize() method
ย 
String and string buffer
String and string bufferString and string buffer
String and string buffer
ย 
Java variable types
Java variable typesJava variable types
Java variable types
ย 
Packages in java
Packages in javaPackages in java
Packages in java
ย 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
ย 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
ย 
Wrapper class
Wrapper classWrapper class
Wrapper class
ย 
Exception Handling
Exception HandlingException Handling
Exception Handling
ย 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
ย 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
ย 

Similar to Threads concept in java

Java Threads: Lightweight Processes
Java Threads: Lightweight ProcessesJava Threads: Lightweight Processes
Java Threads: Lightweight ProcessesIsuru Perera
ย 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in javaElizabeth alexander
ย 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1Hemo Chella
ย 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptxramyan49
ย 
OOP Lecture 20-MultiThreading.pptx
OOP Lecture 20-MultiThreading.pptxOOP Lecture 20-MultiThreading.pptx
OOP Lecture 20-MultiThreading.pptxTanzila Kehkashan
ย 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaJanmejayaPadhiary2
ย 
Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfGouthamSoma1
ย 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024kashyapneha2809
ย 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024nehakumari0xf
ย 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptxHKShab
ย 
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfgiridharsripathi
ย 
multithreadingppt.pptx
multithreadingppt.pptxmultithreadingppt.pptx
multithreadingppt.pptxFardeenAzhar
ย 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxArulmozhivarman8
ย 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreadingRakesh Madugula
ย 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreadingssusere538f7
ย 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptxssuserfcae42
ย 
MULTI THREADING.pptx
MULTI THREADING.pptxMULTI THREADING.pptx
MULTI THREADING.pptxKeerthanaM738437
ย 

Similar to Threads concept in java (20)

Java Threads: Lightweight Processes
Java Threads: Lightweight ProcessesJava Threads: Lightweight Processes
Java Threads: Lightweight Processes
ย 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
ย 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
ย 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
ย 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
ย 
Linux Internals - Part III
Linux Internals - Part IIILinux Internals - Part III
Linux Internals - Part III
ย 
OOP Lecture 20-MultiThreading.pptx
OOP Lecture 20-MultiThreading.pptxOOP Lecture 20-MultiThreading.pptx
OOP Lecture 20-MultiThreading.pptx
ย 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
ย 
Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdf
ย 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024
ย 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024
ย 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
ย 
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdf
ย 
multithreadingppt.pptx
multithreadingppt.pptxmultithreadingppt.pptx
multithreadingppt.pptx
ย 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
ย 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
ย 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
ย 
Java threading
Java threadingJava threading
Java threading
ย 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
ย 
MULTI THREADING.pptx
MULTI THREADING.pptxMULTI THREADING.pptx
MULTI THREADING.pptx
ย 

More from Muthukumaran Subramanian (20)

Struts introduction
Struts introductionStruts introduction
Struts introduction
ย 
Operators in java
Operators in javaOperators in java
Operators in java
ย 
StringTokenizer in java
StringTokenizer in javaStringTokenizer in java
StringTokenizer in java
ย 
Overriding methods
Overriding methodsOverriding methods
Overriding methods
ย 
Oops (inheritance&interface)
Oops (inheritance&interface)Oops (inheritance&interface)
Oops (inheritance&interface)
ย 
Non access modifiers
Non access modifiersNon access modifiers
Non access modifiers
ย 
Jdbc connectivity in java
Jdbc connectivity in javaJdbc connectivity in java
Jdbc connectivity in java
ย 
Garbage collection in java
Garbage collection in javaGarbage collection in java
Garbage collection in java
ย 
Files in java
Files in javaFiles in java
Files in java
ย 
Exception handling
Exception handlingException handling
Exception handling
ย 
Date class
Date classDate class
Date class
ย 
Currency class
Currency classCurrency class
Currency class
ย 
Timer class in java
Timer class in javaTimer class in java
Timer class in java
ย 
Calendar class in java
Calendar class in javaCalendar class in java
Calendar class in java
ย 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
ย 
intorduction to Arrays in java
intorduction to Arrays in javaintorduction to Arrays in java
intorduction to Arrays in java
ย 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
ย 
Abstract classes
Abstract classesAbstract classes
Abstract classes
ย 
Java introduction
Java introductionJava introduction
Java introduction
ย 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
ย 

Recently uploaded

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
ย 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
ย 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
ย 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
ย 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
ย 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
ย 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
ย 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
ย 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
ย 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
ย 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
ย 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
ย 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
ย 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
ย 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
ย 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
ย 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
ย 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
ย 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
ย 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
ย 

Recently uploaded (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
ย 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
ย 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
ย 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
ย 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
ย 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
ย 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
ย 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
ย 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
ย 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
ย 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
ย 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
ย 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
ย 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
ย 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
ย 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
ย 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
ย 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
ย 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
ย 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
ย 

Threads concept in java

  • 1. Multithreading โ— A technique by which a single set of code can be used by several processors at different stages of execution.
  • 2. Process and Threads โ— A process is a thread in execution. โ— A process may be divided ninto number of independent units known as threads. โ— A thread is a dispatchable unit of work. โ— Threads are light-weight processes within a process . โ— A process is a collection of one or more threads and associated system resources
  • 3. Difference between Process and Threads โ— Process can be divided into multiple threads โ— Each process has its own memory space โ— It is difficult to create a process โ— Threads cannot be sub divided. โ— Threads of the same process share a common memory space โ— It is easy to create a thread.
  • 4. Multitasking โ— Multitasking is a method where multiple tasks are performed during the same period of time โ— They are executed concurrently instead of sequentially โ— The tasks share common processing resources, such as a CPU and main memory.
  • 5. Difference between multitasking and multithreading โ— An ability to run several programs simultaneously potentially by using several processors or by time sharing the resources available. โ— An ability to run serveral processes of a single program simultaneously potentially using several processors or by time sharing the resources available.
  • 6. Life cycle of Thread
  • 7. Contd' โ— NEW- A thread that is just instantiated is in new state. When a start() method is invoked, the thread moves to the ready state from which it is automatically moved to runnable state by the thread scheduler. โ— RUNNABLE (ready running)-A thread executing in the JVM is in running state. โ— BLOCKED - A thread that is blocked waiting for a monitor lock is in this state. This can also occur when a thread performs an I/O operation and moves to next (runnable) state. โ— WAITING - A thread that is waiting indefinitely for another thread to perform a particular action is in this state. โ— TIMED_WAITING - A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. โ— TERMINATED - A thread that has exited is in this state.
  • 8. Thread Creation Threads can be created using two ways โ— By implementing the Runnable class โ— By extending the Thread class โ— start() method is used to start the thread โ— run() method is executed after calling the start() โ— run() can contain the code you wish to perform using thread.
  • 9. Some of methods used in Threads โ— start()- to begin the execution of thread โ— setName()- to set the name for a thread โ— getName()- to get the name of a thread โ— Sleep() - to make the thread wait for specified amount of time. โ— setPriority()- to set the priority of the threads โ— getPriority()- to get the priority of threads
  • 10. Thread Creation Example public class Threadlearning extends Thread { public void run() { System.out.println("i am thread "); } public static void main(String[] args) { Threadlearning t = new Threadlearning(); t.start(); } } public class Thread2way implements Runnable { public void run() { System.out.println("i am in thread"); } public static void main(String args[]) { (new Thread(new Thread2way())).start(); } }