Slip No 21 Q A

Write a java program to display name and priority of a Thread.

class MyThread extends Thread {
    public void run() {
        // Display the thread name and priority
        System.out.println("Thread Name: " + Thread.currentThread().getName());
        System.out.println("Thread Priority: " + Thread.currentThread().getPriority());
    }
}

public class ThreadDemo {
    public static void main(String[] args) {
        // Create thread objects
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();

        // Set names
        t1.setName("Thread-A");
        t2.setName("Thread-B");

        // Set priorities
        t1.setPriority(Thread.MIN_PRIORITY); // 1
        t2.setPriority(Thread.MAX_PRIORITY); // 10

        // Start threads
        t1.start();
        t2.start();
    }
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top