class MyThread extends Thread { public void run() { System.out.println("Thread " + Thread.currentThread().getName() + " is running with priority " + Thread.currentThread().getPriority()); } } public class Main { public static void main(String[] args) { MyThread t1 = new MyThread(); MyThread t2 = new MyThread(); t1.setPriority(5); t2.setPriority(8); t1.start(); t2.start(); } }
Thread Thread-0 is running with priority 5 Thread Thread-1 is running with priority 8
class MyThread extends Thread { public void run() { System.out.println("Thread " + Thread.currentThread().getName() + " is running with priority " + Thread.currentThread().getPriority()); } } public class Main { public static void main(String[] args) { MyThread t1 = new MyThread(); t1.setPriority(Thread.MAX_PRIORITY); t1.start(); System.out.println("Thread priority is set to " + t1.getPriority()); } }
Thread Thread-0 is running with priority 10 Thread priority is set to 10This code belongs to the java.lang.Thread package library.