A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/javaexamples/thread_getpri.htm below:

How to get the priorities of running threads in Java

How to get the priorities of running threads in Java Problem Description

How to get the priorities of running threads?

Solution

Following example prints the priority of the running threads by using setPriority() method .

public class SimplePriorities extends Thread {
   private int countDown = 5;
   private volatile double d = 0; 
   public SimplePriorities(int priority) {
      setPriority(priority);
      start();
   }
   public String toString() {
      return super.toString() + ": " + countDown;
   }
   public void run() {
      while(true) {
         for(int i = 1; i < 100000; i++) d = d + (Math.PI + Math.E) / (double)i;
         System.out.println(this);
         if(--countDown == 0) return;
      }
   }
   public static void main(String[] args) {
      new SimplePriorities(Thread.MAX_PRIORITY);
      for(int i = 0; i < 5; i++)
      new SimplePriorities(Thread.MIN_PRIORITY);
   }
} 
Result

The above code sample will produce the following result.

Thread[Thread-0,10,main]: 5
Thread[Thread-0,10,main]: 4
Thread[Thread-0,10,main]: 3
Thread[Thread-0,10,main]: 2
Thread[Thread-0,10,main]: 1
Thread[Thread-1,1,main]: 5
Thread[Thread-1,1,main]: 4
Thread[Thread-1,1,main]: 3
Thread[Thread-1,1,main]: 2
Thread[Thread-1,1,main]: 1
Thread[Thread-2,1,main]: 5
Thread[Thread-2,1,main]: 4
Thread[Thread-2,1,main]: 3
Thread[Thread-2,1,main]: 2
Thread[Thread-2,1,main]: 1
.
.
.

java_threading.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4