Reputation: 383
The following program is supposed to show that the thread having higher priority will take up more of the CPU's time. The code is very similar to the one written in The Complete Reference: Java (seventh edition) by Herbert Schildt(Indian Edition) - Page no 237 & 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 output:
Low priority thread : 708527884 ; High priority thread : 697458303 ; false
Another output:
Low priority thread : 676775494 ; High priority thread : 687116831; true
What could be the reason for this? I have a Macbook Air, with 4GB RAM. Maybe that could be relevant? Please tell me the reason for these inconsistent outputs. Thanks in advance.
Upvotes: 3
Views: 158
Reputation: 131396
You have probably a multi-core CPU with at least two core threads available on your machine during the execution of your program.
If you start less threads than the number of core * thread available by core of your CPU, you could not see any real difference between the thread priorities as both will be executed without need to be paused.
Look at these figures : 708527884
and 697458303
.
you have about 1.5% variation between them. It is almost nothing.
Upvotes: 1
Reputation: 1084
Run it 20 times in a row, and the higher priority thread should get the higher number most of the time. Thread priority settings are fairly loose, and giving one thread a higher priority doesn't guarantee more attention on a 10 second interval. It will get more attention on average through a long period of time.
Upvotes: 0
Reputation: 182769
Your two threads don't compete with each other, so their priorities don't matter. On the other hand, the low priority thread is running while you're creating the high priority thread, so it probably runs for a bit longer.
the thread having higher priority will take up more of the CPU's time
That higher priority makes things run faster or run more is a common misunderstanding. These two threads don't compete with each other, so which one wins if they compete makes no difference. Priority doesn't make things go faster, it just gives them preference where such a thing matters.
The reason for the inconsistent outputs is likely that the amount of time that one threads runs when the other doesn't is highly dependent on exactly what else the system is doing at the time.
Upvotes: 2