• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Needs Explanation

 
Venkat Ramsimha
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
please anybody let me know as how the output is generated for the below program

class A44 extends Thread
{
static long startTime;
public void run()
{
for (int i = 0; i < 99999; i++)
{
Math.sin(i);
}
String name = Thread.currentThread().getName();
long time = System.currentTimeMillis();
System.out.println(name + " done at " + (time - startTime));
}
public static void main(String[] args)
{
A44 t1 = new A44();
A44 t2 = new A44();
t1.setName("T1");
t2.setName("T2");
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
startTime = System.currentTimeMillis();
t1.start();
t2.start();
}
}

Thanks,
venkatramsimha
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When multiple threads are ready to start the execution jvm chooses the runnable thread with highest priority for execution. If this thread is stop/yield/or becomes not runnable for some other reason then the low priority thread is executed.


In your code thread t2 has highest priority and thread t1 has lowest priority. So jvm starts thread t2 first. When the thread t2 is stopped then only the thread t1 is executed.
So your code out put is like below
T2 done at xx
T1 done at xx
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ragu
what i thought is that the thread priority level will come only when the both threads are in runnable state. there is a chance the first thread t1 will get the execution of run method. When one thread running i doubt the thread with high priority will access the sheduler since thread 1 is running.
 
Maxime Duval
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I think that whenever a thread with a higher priority come in the Runnable state, usually, the JVM will stop the thread he is running to do the higher priority one. Thats why in this case, T2 will finish first even if T1 is started first.

Maxime
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two points...

First, there are more than two threads, don't forget the main thread that is starting these two threads. In this scenario...

When the main thread starts the low priority thread nothing should happen, as the main thread has a higher priority than the newly started thread. When the main thread starts the high priority thread, that should cause a prempt to the newly created thread. Once the high priority thread finishes, that should cause a switch back to the main thread. Once the main thread finishes, that should cause a switch to the low priority thread.

Second, threading is susceptible to the underlying threading model. This means that for windows, the priorities are actually adjusted based on the current time slice, last time slice, etc. The high and low priority threads will actually round robin, in the long run -- albeit, it shouldn't be distributed evenly.

Henry
 
Maxime Duval
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the JVM use Round Robin, how is a high priority thread advantaged compared to a low one? Will it still get more cpu time or will all the thread get equal time slice?

Maxime
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maxime Duval:
If the JVM use Round Robin, how is a high priority thread advantaged compared to a low one? Will it still get more cpu time or will all the thread get equal time slice?

Maxime


Under Windows, a higher priority thread should get more CPU time than a lower priority thread. The main reason for this is to prevent thread starvation.

Henry
 
Maxime Duval
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry ! Do you know of any major OS who dont use Round Robin to select witch task should run?
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maxime Duval:
Thanks Henry ! Do you know of any major OS who dont use Round Robin to select witch task should run?


Most modern OSes today will time slice. However, Windows is not the norm. Most Unix variants (Solaris and Linux) will tend to time slice only within the same priority -- hence, it is possible for a high priority thread to starve a low priority thread.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic