Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.8 kB
1
Indexable
Never
class MyThreadClass extends Thread {
    public void run(){
        try {
            setPriority(Thread.MAX_PRIORITY);
            System.out.println("Currently Executing Thread : " + this.currentThread().getName());
            System.out.println("Thread priority : " + this.getPriority());
            Thread.sleep(1000);
            System.out.println("Thread priority : " + this.getPriority());
            wait();
            System.out.println("Thread priority : " + this.getPriority());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class MyRunnable implements Runnable {
    public void run(){
        try {
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
            System.out.println("Currently Executing Thread : " + Thread.currentThread().getName());
            System.out.println("Thread priority : " + Thread.currentThread().getPriority());
            Thread.sleep(1000);
            System.out.println("Thread priority : " + Thread.currentThread().getPriority());
            synchronized(this){
                wait();
            }
            System.out.println("Thread priority : " + Thread.currentThread().getPriority());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        MyThreadClass t1 = new MyThreadClass();
        MyRunnable r1 = new MyRunnable();
        t1.start();
        Thread t2 = new Thread(r1);
        t2.start();
        System.out.println("Main thread priority : " + Thread.currentThread().getPriority());
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        System.out.println("Main thread priority : " + Thread.currentThread().getPriority());
    }
}
Leave a Comment