Forums Register Login

Needs Explanation

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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

 
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
Thanks Henry ! Do you know of any major OS who dont use Round Robin to select witch task should run?
+Pie Number of slices to send: Send
 

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
What? What, what, what? What what tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 789 times.
Similar Threads
Thread Priority issue
thread priority
Thread's Priority
Explain output of this program
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:49:56.