- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Change Thread Priority 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 determines when the processor is provided to the thread as well as other resources. It can be changed using the method setPriority() of class Thread.
A program that demonstrates changing the thread priorities using the method setPriority() 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()); System.out.println("
Changing the default thread priority using the setPriority() method"); thread1.setPriority(7); thread2.setPriority(3); thread3.setPriority(9); thread4.setPriority(2); thread5.setPriority(8); System.out.println("
Changed thread priority of Thread 1: " + thread1.getPriority()); System.out.println("Changed thread priority of Thread 2: " + thread2.getPriority()); System.out.println("Changed thread priority of Thread 3: " + thread3.getPriority()); System.out.println("Changed thread priority of Thread 4: " + thread4.getPriority()); System.out.println("Changed thread priority of Thread 5: " + thread5.getPriority()); System.out.println("
" + Thread.currentThread().getName()); System.out.println("
Default thread priority of Main Thread: " + Thread.currentThread().getPriority()); Thread.currentThread().setPriority(10); System.out.println("Changed 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 Changing the default thread priority using the setPriority() method Changed thread priority of Thread 1: 7 Changed thread priority of Thread 2: 3 Changed thread priority of Thread 3: 9 Changed thread priority of Thread 4: 2 Changed thread priority of Thread 5: 8 main Default thread priority of Main Thread: 5 Changed thread priority of Main Thread: 10
- Related Articles
- Java Thread Priority in Multithreading
- Importance of Thread Priority in Java?
- How to get current thread priority in android?
- C# Program to display priority of Thread
- How to use set the priority for a thread in android?
- User Thread vs Daemon Thread in Java?
- Thread Pools in Java
- Daemon thread in Java
- How a thread can interrupt another thread in Java?
- Minimum and Maximum Priority Threads in Java
- Naming a thread in Java
- Inter thread communication in Java
- Demonstrate thread priorities in Java
- Get current thread in Java
- Thread Interference Error in Java

Advertisements