SlideShare a Scribd company logo
Use the Java Thread class to demonstrate the following functionalities:
(1)
Create five threads; show the id for each thread – getId()
(2)
Show the use of the following methods (be creative in your code):
start(), sleep(), yield(), getState(), setName(), getName(),
setPriority(), getPriority() -- Make sure to show threads are processed
with different priority (be creative)
* As a minimum, discuss the conditions resulted from exceptions – examine the Throws section
of each method, to base your code discussions.
Solution
//start(), sleep(), yield(), setName(), setPriority(),
class Demo implements Runnable
{
public void run()
{
//Thread State
Thread.State state = Thread.currentThread().getState();
//Thread ID
System.out.println("Thread Id = " + Thread.currentThread().getId());
//Thread Name
System.out.println("Thread Name = " + Thread.currentThread().getName());
//Thread Priority
System.out.println("Thread Priority = " + Thread.currentThread().getPriority());
//Thread State Display
System.out.println("Thread State = " + state);
//Yields the current thread
Thread.yield();
//Exception handling
try
{
//Takes the thread to sleep
Thread.sleep(2000);
}
catch(InterruptedException ie)
{
System.out.println(Thread.currentThread());
}
}
}
public class threadDemo
{
public static void main(String ss[])
{
//Creates 5 objects
Demo td1 = new Demo();
Demo td2 = new Demo();
Demo td3 = new Demo();
Demo td4 = new Demo();
Demo td5 = new Demo();
//Creates 5 threads and assigns object to it
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
Thread t3 = new Thread(td3);
Thread t4 = new Thread(td4);
Thread t5 = new Thread(td5);
//Sets the name and priority of each thread
t1.setName("First"); t1.setPriority(3);
t2.setName("Second"); t2.setPriority(6);
t3.setName("Third"); t3.setPriority(8);
t4.setName("Fourth"); t4.setPriority(1);
t5.setName("Fifth"); t4.setPriority(2);
//Stats each thread
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
Output
Thread Id = 12
Thread Name = Third
Thread Priority = 8
Thread State = RUNNABLE
Thread Id = 14
Thread Id = 11
Thread Name = Fifth
Thread Name = Second
Thread Priority = 5
Thread Id = 13
Thread Id = 10
Thread Priority = 6
Thread State = RUNNABLE
Thread Name = First
Thread Priority = 3
Thread State = RUNNABLE
Thread Name = Fourth
Thread State = RUNNABLE
Thread Priority = 2
Thread State = RUNNABLE

More Related Content

PPT
multhi threading concept in oops through java
PPTX
Multithreading
PPTX
Java Multithreading.pptx
PPTX
Multithreading.pptx
PPTX
OOPS object oriented programming UNIT-4.pptx
PPTX
Multi threading
PDF
Python programming : Threads
PDF
Threads
multhi threading concept in oops through java
Multithreading
Java Multithreading.pptx
Multithreading.pptx
OOPS object oriented programming UNIT-4.pptx
Multi threading
Python programming : Threads
Threads

Similar to Use the Java Thread class to demonstrate the following functionaliti.pdf (20)

PDF
Java for beginners
PPTX
Internet Programming with Java
PPTX
Threading concepts
PPT
java multi threading and synchronisation.ppt
PPT
Lec7!JavaThreads.ppt java multithreading
PPT
Lec7!JavaThreads.ppt
PDF
Java programming basics
PPTX
Java programming PPT. .pptx
PDF
Spock: Test Well and Prosper
PPT
JAVA MULTITHREDED PROGRAMMING - LECTURES
PDF
Jvm internals
PPTX
Concept of Java Multithreading-Partially.pptx
PPTX
multithreading to be used in java with good programs.pptx
PPTX
Java Threads And Concurrency Presentation. 2024
PPTX
Java-Threads And Concurrency Presentation. 2024
PDF
Java unit 12
PPTX
Multithreading in Java Object Oriented Programming language
PPT
multithreading, creating a thread and life cycle in java.ppt
PPT
ThreadProperties
Java for beginners
Internet Programming with Java
Threading concepts
java multi threading and synchronisation.ppt
Lec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt
Java programming basics
Java programming PPT. .pptx
Spock: Test Well and Prosper
JAVA MULTITHREDED PROGRAMMING - LECTURES
Jvm internals
Concept of Java Multithreading-Partially.pptx
multithreading to be used in java with good programs.pptx
Java Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024
Java unit 12
Multithreading in Java Object Oriented Programming language
multithreading, creating a thread and life cycle in java.ppt
ThreadProperties

More from arjuntiwari586 (20)

PDF
Would you recomend that Miss E begin to take a vitamin or mineral su.pdf
PDF
Which perspective best describes adolescenceWhen does adolescence.pdf
PDF
Which of the following is a trace element, required only in small amo.pdf
PDF
Which of the following enzymes has the ability to accomplish all thr.pdf
PDF
What risk are associated with pregnancySolutionThere are vari.pdf
PDF
What are the relative signal intensities for Grey and White matter u.pdf
PDF
What is the ovum doing during the primary oocyte stageSolution.pdf
PDF
What is the structural difference between the sugar found in RNA and.pdf
PDF
uìdyov fotect peaga e (or ways co profecé se p protect e . Haa .pdf
PDF
Turner suppressor genes.... prevent gene expression of oncogenes. pre.pdf
PDF
This image is a reminder that statistical significant is often set at.pdf
PDF
The state diagram shown up corresponds to a circuit implementation th.pdf
PDF
The lineage that produced chimpanzees and humans split from other pri.pdf
PDF
production deviance postconventional level primary stakeholders perso.pdf
PDF
Question 19 (1 point) The DuPont identity breaks down return on equit.pdf
PDF
Prepare a 3-4 page, double-spaced paper (cite 3-4 reliable sources) .pdf
PDF
need help with this java lab please read below and follow the steps.pdf
PDF
Place all the characteristics of living organisms the answer box. Ch.pdf
PDF
LABELING ACTIVITY 2 (Rs 1-10) 10 10. 12. 13 14, 15 16 62 circulatory .pdf
PDF
Of 10 fish in a pond 4 are tagged. If three fish are caught and not .pdf
Would you recomend that Miss E begin to take a vitamin or mineral su.pdf
Which perspective best describes adolescenceWhen does adolescence.pdf
Which of the following is a trace element, required only in small amo.pdf
Which of the following enzymes has the ability to accomplish all thr.pdf
What risk are associated with pregnancySolutionThere are vari.pdf
What are the relative signal intensities for Grey and White matter u.pdf
What is the ovum doing during the primary oocyte stageSolution.pdf
What is the structural difference between the sugar found in RNA and.pdf
uìdyov fotect peaga e (or ways co profecé se p protect e . Haa .pdf
Turner suppressor genes.... prevent gene expression of oncogenes. pre.pdf
This image is a reminder that statistical significant is often set at.pdf
The state diagram shown up corresponds to a circuit implementation th.pdf
The lineage that produced chimpanzees and humans split from other pri.pdf
production deviance postconventional level primary stakeholders perso.pdf
Question 19 (1 point) The DuPont identity breaks down return on equit.pdf
Prepare a 3-4 page, double-spaced paper (cite 3-4 reliable sources) .pdf
need help with this java lab please read below and follow the steps.pdf
Place all the characteristics of living organisms the answer box. Ch.pdf
LABELING ACTIVITY 2 (Rs 1-10) 10 10. 12. 13 14, 15 16 62 circulatory .pdf
Of 10 fish in a pond 4 are tagged. If three fish are caught and not .pdf

Recently uploaded (20)

PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Types and Its function , kingdom of life
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Cell Structure & Organelles in detailed.
Paper A Mock Exam 9_ Attempt review.pdf.
What if we spent less time fighting change, and more time building what’s rig...
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Types and Its function , kingdom of life
Yogi Goddess Pres Conference Studio Updates
Chinmaya Tiranga quiz Grand Finale.pdf
Final Presentation General Medicine 03-08-2024.pptx
Computing-Curriculum for Schools in Ghana
Microbial diseases, their pathogenesis and prophylaxis
Microbial disease of the cardiovascular and lymphatic systems
Orientation - ARALprogram of Deped to the Parents.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE

Use the Java Thread class to demonstrate the following functionaliti.pdf

  • 1. Use the Java Thread class to demonstrate the following functionalities: (1) Create five threads; show the id for each thread – getId() (2) Show the use of the following methods (be creative in your code): start(), sleep(), yield(), getState(), setName(), getName(), setPriority(), getPriority() -- Make sure to show threads are processed with different priority (be creative) * As a minimum, discuss the conditions resulted from exceptions – examine the Throws section of each method, to base your code discussions. Solution //start(), sleep(), yield(), setName(), setPriority(), class Demo implements Runnable { public void run() { //Thread State Thread.State state = Thread.currentThread().getState(); //Thread ID System.out.println("Thread Id = " + Thread.currentThread().getId()); //Thread Name System.out.println("Thread Name = " + Thread.currentThread().getName()); //Thread Priority System.out.println("Thread Priority = " + Thread.currentThread().getPriority()); //Thread State Display System.out.println("Thread State = " + state); //Yields the current thread Thread.yield(); //Exception handling try { //Takes the thread to sleep Thread.sleep(2000);
  • 2. } catch(InterruptedException ie) { System.out.println(Thread.currentThread()); } } } public class threadDemo { public static void main(String ss[]) { //Creates 5 objects Demo td1 = new Demo(); Demo td2 = new Demo(); Demo td3 = new Demo(); Demo td4 = new Demo(); Demo td5 = new Demo(); //Creates 5 threads and assigns object to it Thread t1 = new Thread(td1); Thread t2 = new Thread(td2); Thread t3 = new Thread(td3); Thread t4 = new Thread(td4); Thread t5 = new Thread(td5); //Sets the name and priority of each thread t1.setName("First"); t1.setPriority(3); t2.setName("Second"); t2.setPriority(6); t3.setName("Third"); t3.setPriority(8); t4.setName("Fourth"); t4.setPriority(1); t5.setName("Fifth"); t4.setPriority(2); //Stats each thread t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); }
  • 3. } Output Thread Id = 12 Thread Name = Third Thread Priority = 8 Thread State = RUNNABLE Thread Id = 14 Thread Id = 11 Thread Name = Fifth Thread Name = Second Thread Priority = 5 Thread Id = 13 Thread Id = 10 Thread Priority = 6 Thread State = RUNNABLE Thread Name = First Thread Priority = 3 Thread State = RUNNABLE Thread Name = Fourth Thread State = RUNNABLE Thread Priority = 2 Thread State = RUNNABLE