Question

In: Computer Science

3. Write a Java program to demonstrate the concept of priority thread. It should show that...

3. Write a Java program to demonstrate the concept of priority thread. It should show that each thread have a priority. Priorities are represented by a number between 1 and 20. In most cases, thread schedular schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.


3 constants defined in Thread class:
1. public static int MIN_PRIORITY
2. public static int NORM_PRIORITY
3. public static int MAX_PRIORITY

Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and
the value of MAX_PRIORITY is 20.

Here is an example of priority of a Thread:
1. class TestMultiPriority1 extends Thread{
2. public void run(){
3.    System.out.println("running thread name is:"+Thread.currentThread().getName());
4.    System.out.println("running thread priority is:"+Thread.currentThread().getPriority());
5.   }

6. public static void main(String args[]){
7.   TestMultiPriority1 m1=new TestMultiPriority1();
8.   TestMultiPriority1 m2=new TestMultiPriority1();
9.   m1.setPriority(Thread.MIN_PRIORITY);
10.   m2.setPriority(Thread.MAX_PRIORITY);
11.   m1.start();
12.   m2.start();
13.  
14. }
15. }

Solutions

Expert Solution

public class TestMultiPriority1 extends Thread {
   public void run() {
       // printing the name and Priority of current running thread
       System.out.println(
               "Name : " + Thread.currentThread().getName() + " : Priority : " + Thread.currentThread().getPriority());
   }

   public static void main(String args[]) {
       TestMultiPriority1 m1 = new TestMultiPriority1();
       TestMultiPriority1 m2 = new TestMultiPriority1();
       m1.setPriority(Thread.MIN_PRIORITY);
       m2.setPriority(Thread.MAX_PRIORITY);
       m1.start();
       m2.start();

   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at least three user-threads. 2. Each thread needs to be terminated after printing each thread ID.
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should be only 3) in it and prints out the number of photos in the gallery.
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Using Java, define a train class and write code to demonstrate its functionality The train should...
Using Java, define a train class and write code to demonstrate its functionality The train should run between Boston and New York, stopping at different, different station along the way. Provide output to indicate the train’s current status.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT