aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Needs Explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Needs Explanation" Watch "Needs Explanation" New topic
Author

Needs Explanation

Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
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

Joined: Mar 18, 2005
Posts: 143
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


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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

Joined: Apr 27, 2005
Posts: 7
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
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Maxime Duval
Greenhorn

Joined: Apr 27, 2005
Posts: 7
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
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

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

Joined: Apr 27, 2005
Posts: 7
Thanks Henry ! Do you know of any major OS who dont use Round Robin to select witch task should run?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Needs Explanation