- Java Programming Examples
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to check priority level of a thread in Java
Problem Description
How to check priority level of a thread?
Solution
Following example demonstrates how to check priority level of a thread using getPriority() method of a Thread.
public class Main extends Object { private static Runnable makeRunnable() { Runnable r = new Runnable() { public void run() { for (int i = 0; i < 5; i++) { Thread t = Thread.currentThread(); System.out.println("in run() - priority = " + t.getPriority()+ ", name = " + t.getName()); try { Thread.sleep(2000); } catch (InterruptedException x) {} } } }; return r; } public static void main(String[] args) { System.out.println( "in main() - Thread.currentThread(). getPriority() = " + Thread.currentThread().getPriority()); System.out.println( "in main() - Thread.currentThread().getName() = "+ Thread.currentThread().getName()); Thread threadA = new Thread(makeRunnable(), "threadA"); threadA.start(); try { Thread.sleep(3000); } catch (InterruptedException x) {} System.out.println("in main() - threadA.getPriority() = " + threadA.getPriority()); } }
Result
The above code sample will produce the following result.
in main() - Thread.currentThread().getPriority() = 5 in main() - Thread.currentThread().getName() = main in run() - priority = 5, name = threadA in run() - priority = 5, name = threadA in main() - threadA.getPriority() = 5 in run() - priority = 5, name = threadA in run() - priority = 5, name = threadA in run() - priority = 5, name = threadA
java_threading.htm
Advertisements