0

I was learning Multithreading in Java and though to measure the relative CPU usage of two threads hi and lo on the below code.

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

public Clicker(int p)
{
    t=new Thread(this);
    t.setPriority(p);
}
public void run(){
    while(running){
        click++;
        }
}

public void stop(){
    running =false;
}

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


public class HiLoPri {

  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(InterruptedException e){
        System.out.println("Main Thread interrupted. ");
        }

    lo.stop();
    hi.stop();

    try{
        hi.t.join();
        lo.t.join();
    }catch(InterruptedException e){
        System.out.println("Interrupted Exception Caught");
    }

    System.out.println("Low priority : " + lo.click);
    System.out.println("High priority : " + hi.click);
         }

}

Here's are the outputs for various priorities:

  1. lo = NORM_PRIORITY -2 and hi = NORM_PRIORITY +2 : Low priority : 1725664879, High priority : 1774465713 || High/Low = 1.02827
  2. lo = NORM_PRIORITY -4 and hi = NORM_PRIORITY +4 : Low priority : 2142378792, High priority : 2180156175 || High/Low = 1.01763
  3. lo = NORM_PRIORITY and hi = NORM_PRIORITY : Low priority : 2582216343 , High priority : 2581415280 || High/Low = 0.99968

From output 3, I understood in two threads of equal priority, the first one gets slighly more peference.

In the case of output 1 and output 2, see the priority values. When the priority difference became high, counts incresed. But when I set the difference 0 (in output 3), as contrary to the above observation, counts shows an increase instead of descrease.

Can you explain why?

(Specs : Java SE 7, AMD A10 quad core 2.3GHz and Window 8)

user3298129
  • 59
  • 1
  • 5
  • It's multi-threading and priority doesn't decide any thing about the outputs. It increases the probability but doesn't guarantee about it. – Braj Jun 26 '14 at 18:46
  • 1
    Priorities really only come into play when there's contention for CPU time. On a quad core system, that's not even all that significant a factor til the system's trying to do 4 CPU-bound tasks simultaneously. – cHao Jun 26 '14 at 18:46
  • Can you explain what you expected to happen and why? Are you sure you get this result *every time* you do this? – Peter Lawrey Jun 26 '14 at 19:01

2 Answers2

1

The Java spec does not guarantee that priorities are considered when assigning quota to threads.

Every thread has a priority. When there is competition for processing resources, threads with higher priority are generally executed in preference to threads with lower priority. Such preference is not, however, a guarantee that the highest priority thread will always be running, and thread priorities cannot be used to reliably implement mutual exclusion.

Andres
  • 10,561
  • 4
  • 45
  • 63
0

Thread priority is just a hint to the OS. This hint, if it does anything at all, will help the OS decide which threads should run when you have more threads needing to run than you have logical CPUs. If you have plenty of free CPU, even the lowest priority thread will run as much as it wants.

Note: to raise the priority of a thread on Windows you need to be an Administrator and on Unix you need to be root.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130