SlideShare a Scribd company logo
1 of 3
Download to read offline
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

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

Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
 

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

Internet Programming with Java
Internet Programming with JavaInternet Programming with Java
Internet Programming with Java
 
Java
JavaJava
Java
 
Threading concepts
Threading conceptsThreading concepts
Threading concepts
 
Lec7!JavaThreads.ppt
Lec7!JavaThreads.pptLec7!JavaThreads.ppt
Lec7!JavaThreads.ppt
 
Lec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreadingLec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreading
 
Lec7!JavaThreads.ppt
Lec7!JavaThreads.pptLec7!JavaThreads.ppt
Lec7!JavaThreads.ppt
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
 
Java programming PPT. .pptx
Java programming PPT.                 .pptxJava programming PPT.                 .pptx
Java programming PPT. .pptx
 
Spock: Test Well and Prosper
Spock: Test Well and ProsperSpock: Test Well and Prosper
Spock: Test Well and Prosper
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
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
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
 
multithreading, creating a thread and life cycle in java.ppt
multithreading, creating a thread and life cycle in java.pptmultithreading, creating a thread and life cycle in java.ppt
multithreading, creating a thread and life cycle in java.ppt
 
ThreadProperties
ThreadPropertiesThreadProperties
ThreadProperties
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Mastering Java ByteCode
Mastering Java ByteCodeMastering Java ByteCode
Mastering Java ByteCode
 
unit-3java.pptx
unit-3java.pptxunit-3java.pptx
unit-3java.pptx
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
 

More from arjuntiwari586

Which of the following is a trace element, required only in small amo.pdf
Which of the following is a trace element, required only in small amo.pdfWhich of the following is a trace element, required only in small amo.pdf
Which of the following is a trace element, required only in small amo.pdf
arjuntiwari586
 
The lineage that produced chimpanzees and humans split from other pri.pdf
The lineage that produced chimpanzees and humans split from other pri.pdfThe lineage that produced chimpanzees and humans split from other pri.pdf
The lineage that produced chimpanzees and humans split from other pri.pdf
arjuntiwari586
 
Prepare a 3-4 page, double-spaced paper (cite 3-4 reliable sources) .pdf
Prepare a 3-4 page, double-spaced paper (cite 3-4 reliable sources) .pdfPrepare a 3-4 page, double-spaced paper (cite 3-4 reliable sources) .pdf
Prepare a 3-4 page, double-spaced paper (cite 3-4 reliable sources) .pdf
arjuntiwari586
 
need help with this java lab please read below and follow the steps.pdf
need help with this java lab please read below and follow the steps.pdfneed help with this java lab please read below and follow the steps.pdf
need help with this java lab please read below and follow the steps.pdf
arjuntiwari586
 

More from arjuntiwari586 (20)

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

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 

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