Question

class A extends Thread {
    public void run() {
        for (int i = 1; i < 5; i++) {
            System.out.println("Thread A Loop NO " + i);

        }
        System.out.println("Exit from A");
    }
}

class B extends Thread {
    public void run() {
        for (int j = 1; j < 5; j++) {
            System.out.println("Thread B Loop no. " + j);
        }
        System.out.println("Exit from B");
    }
}

class C extends Thread {
    public void run() {
        for (int k = 1; k < 5; k++) {
            System.out.println("Thread C Loop no " + k);
        }
        System.out.println("Exit Form c");
    }
}

class Demo1 {
    public static void main(String args[]) {
        A a1 = new A();
        B b1 = new B();
        C c1 = new C();

        c1.setPriority(Thread.MAX_PRIORITY);
        a1.start();
        b1.start();
        c1.start();
    }
}  

I ran this program several times. Sometimes the thread "C" with max priority completes at the last Does the max priority not ensure it to terminate first? I mean before any other thread exits from the loop? If not what is the scheduler policy?

Was it helpful?

Solution

Does the max priority doesnot ensure it to terminate first????, i mean befor any other thread exits from the loop???

No, It doesn't ensure anything.

if not what is the scheduler policy???

Its a hint to the OS which it is free to ignore esp if you are not a privileged user.

Even if it did do what you suggest its a bad idea to depend on itself behaviour too much. i.e. I would always make sure your program behaves correctly without it.

If you want Thread C to complete first you should run it first e.g. with c1.run();

OTHER TIPS

Setting a Thread's priority to Max doesn't ensure that it will end in last.. It is just used as an indicator, that this thread should be chosen over the others, if required at times, suppose when, the CPU is free, and some threads are waiting to get it.. So, in this case, if you consider Priority based Scheduling, your thread with maximum priority will get the CPU for sure..

But, if you want to ensure that your Thread C ends last, you can invoke a Thread.join() method on this thread to force other Threads, to end after Thread C.. ( But, a thread can only join on the threads which that thread has spawned, Because it won't have reference to the other thread that were spawned from some other Thread.)

Apart from the other points raised, your threads do next-to-nothing except acquire and release locks on the output stream. Even if you had only one CPU so that only the highest-priority ready thread could run, I doubt that you could say anything definitive about which thread would complete first. Changing priorities is an optimization to improve some aspect/s of performance on a box that is overloaded. It is abolutely not any kind of synchronization mechanism and cannot be used to turn an app that does not work at all into one that does. The best that can be said is that it may well make an app pass some performance threshold in a requirement spec.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top