-1
class MyThread extends Thread {

    public MyThread(String name) {
        super(name); //setName(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(getName()+" : "+i);
        }
    }
}

public class Demo {
    public static void main(String args[]){
        MyThread t1=new MyThread("One");
        MyThread t2=new MyThread("Two");
        MyThread t3=new MyThread("Three");
        System.out.println("t1 : "+t1.getPriority()); //5
        System.out.println("t2 : "+t2.getPriority()); //5
        System.out.println("t3 : "+t3.getPriority()); //5

        t1.setPriority(1);  //Thread.MIN_PRIORITY
        t2.setPriority(5);  //Thread.NORM_PRIORITY
        t3.setPriority(10); //Thread.MAX_PRIORITY

        t1.start();
        t2.start();
        t3.start();
    }
}

I wrote this code to check the priority in java threads. But It doesn't give the priority in every time this code execute. Please explain me why this happens? Is this the nature of Java threads or else?

  • What do you mean by "it doesn't give the priority"? Were you expecting the entire rest of the system to sit idle and just let the highest-priority thread work while the rest of the system's capacity is wasted? High-priority work doesn't stop low-priority work until it's done. High-priority doesn't make something faster either, if it did, we'd always make everything high-priority. And low-priority doesn't make things slower or we'd never use it. – David Schwartz Dec 16 '18 at 09:14

1 Answers1

1

If there are more cores than number of threads then there would not be any effect as all threads can be given CPU . Also even it seems like, but all threads are not deployed at same time.