- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Demonstrate thread priorities in Java
A thread can be created by implementing the Runnable interface and overriding the run() method. Then a Thread object can be created and the start() method called.
The thread priority can be provided to the thread by the JVM or the programmer. It determines when the processor is provided to the thread as well as other resources. The method setPriority() of class Thread can be used to set the priority of the thread.
A program that demonstrates thread priorities in Java is given as follows:
Example
public class ThreadDemo extends Thread { public void run() { System.out.println("Running..."); } public static void main(String[] args) { ThreadDemo thread1 = new ThreadDemo(); ThreadDemo thread2 = new ThreadDemo(); ThreadDemo thread3 = new ThreadDemo(); ThreadDemo thread4 = new ThreadDemo(); ThreadDemo thread5 = new ThreadDemo(); System.out.println("Default thread priority of Thread 1: " + thread1.getPriority()); System.out.println("Default thread priority of Thread 2: " + thread2.getPriority()); System.out.println("Default thread priority of Thread 3: " + thread3.getPriority()); System.out.println("Default thread priority of Thread 4: " + thread4.getPriority()); System.out.println("Default thread priority of Thread 5: " + thread5.getPriority()); thread1.setPriority(8); thread2.setPriority(1); thread3.setPriority(6); thread4.setPriority(9); thread5.setPriority(3); System.out.println("Modified thread priority of Thread 1: " + thread1.getPriority()); System.out.println("Modified thread priority of Thread 2: " + thread2.getPriority()); System.out.println("Modified thread priority of Thread 3: " + thread3.getPriority()); System.out.println("Modified thread priority of Thread 4: " + thread4.getPriority()); System.out.println("Modified thread priority of Thread 5: " + thread5.getPriority()); System.out.print(Thread.currentThread().getName()); System.out.println("
Default thread priority of Main Thread: " + Thread.currentThread().getPriority()); Thread.currentThread().setPriority(10); System.out.println("Modified thread priority of Main Thread: " + Thread.currentThread().getPriority()); } }
Output
Default thread priority of Thread 1: 5 Default thread priority of Thread 2: 5 Default thread priority of Thread 3: 5 Default thread priority of Thread 4: 5 Default thread priority of Thread 5: 5 Modified thread priority of Thread 1: 8 Modified thread priority of Thread 2: 1 Modified thread priority of Thread 3: 6 Modified thread priority of Thread 4: 9 Modified thread priority of Thread 5: 3 main Default thread priority of Main Thread: 5 Modified thread priority of Main Thread: 10
- Related Articles
- Demonstrate Static Import in Java
- User Thread vs Daemon Thread in Java?
- Demonstrate the clone() method in Java
- Demonstrate Private access modifier in Java
- Daemon thread in Java
- Thread Pools in Java
- How a thread can interrupt another thread in Java?
- Demonstrate constructors in a Multilevel Hierarchy in Java
- Create and demonstrate an immutable collection in Java
- Demonstrate getting the immediate superclass information in Java
- Demonstrate static variables, methods and blocks in Java
- Inter thread communication in Java
- Get current thread in Java
- Change Thread Priority in Java
- Java Thread Priority in Multithreading

Advertisements