Inconsistent output with thread priority program

The following program should show that the higher priority thread will take more CPU time. The code is very similar to the one written in The Complete Reference: Java (7th Edition) by Herbert Schildt (Indian Edition) - Pages 237 and 238.

class clicker implements Runnable
{
    long click=0;
    Thread t;
    private volatile boolean running=true;

    public clicker(int p)
    {
        t=new Thread(this,"yo yo");
        t.setPriority(p);
    }

    public void run()
    {
        while(running)
            {click++;}
    }
    public void stop()
    {
        running = false;
    }

    public void start()
    {
        t.start();
    }
}
public class ThreadPriorities {

    public static void main(String[] args) {
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        clicker hi=new clicker(Thread.NORM_PRIORITY+2);
        clicker lo=new clicker(Thread.NORM_PRIORITY-2);

        lo.start();
        hi.start();

        try{Thread.sleep(10000);}catch(Exception e){}       

        lo.stop();
        hi.stop();
        try{
            hi.t.join();
            lo.t.join();
        }catch(Exception e){}

        System.out.println("Low priority thread : "+lo.click);
        System.out.println("High priority thread : "+hi.click);
        System.out.println(lo.click<hi.click?true:false);
    }

}

      

One conclusion:

Low priority stream: 708527884; High priority stream: 697458303; False

Another conclusion:

Low priority topic: 676775494; High priority thread: 687116831; true

What could be causing this? I have a Macbook Air with 4GB of RAM. Maybe it might be relevant? Please tell me the reason for these inconsistent results. Thanks in advance.

+3


source to share


3 answers


Your two threads are not in competition with each other, so their priorities are irrelevant. On the other hand, a low priority thread runs as long as you create a high priority thread, so it probably runs a little longer.

higher priority thread will take longer CPU time



This higher priority makes things run faster or run more - this is a common misunderstanding. These two streams do not compete with each other, so it does not matter if they compete wins. Priority doesn't speed things up, it just gives them preference when it matters.

The reason for inconsistent outputs is the likelihood that the time one thread is running when another is down is highly dependent on what else the system is doing at the moment.

+2


source


You probably have a multi-core processor with at least two main threads available on your computer while your program is running.
If you run fewer threads than the number of kernel threads * available across your processor's core, you won't be able to see the real difference between thread priorities, as both will be executed without the need to suspend.



Look at these numbers: 708527884

and 697458303

.
you have about 1.5% difference between them. It's almost nothing.

+1


source


Run it 20 times in a row, and the higher priority thread should get the higher number for more time. The thread priority settings are fairly loose, and when using a single thread, higher priority does not guarantee more attention on a 10 second interval. He will receive more attention on average over a long period of time.

0


source







All Articles