views:

92

answers:

5

A java process starts 5 threads , each thread takes 5 minutes. what will be the minimum and maximum time taken by process? will be of great help if one can explain in java threads and OS threads.

Edit : I want to know how java schedule threads at OS level.

+4  A: 

This depends on the amount of logical processor cores you have and the already running processes and the priority of the threads. The theoretical minimum would be 5 minutes plus the little overhead in starting and controlling threads, if you have at least five logical processor cores. The theoretical maximum would be 25 minutes plus the little overhead, if you have only one logical processor core available. The mentioned overhead is usually not more than a few milliseconds.

The theoretical maximum can however be unpredictably (much) higher if there are at the same time a lot of another running threads with a higher priority from other processes than the JVM.

Edit : I want to know how java schedule threads at OS level.

The JVM just spawns another native thread and it get assigned to the process associated with JVM itself.

BalusC
On the single processor core wouldn't that be a theoretically minimum (not maximum) of 25 minutes?
Kevin Brock
@Kevin: from another point of view, yes :)
BalusC
+1  A: 

Minimum time, 5 minutes, assuming that threads run entirely concurrently with no interdependencies and have a dedicated core available. Maximum time, 25 minutes, assuming that each thread has to have exclusive use of some global resource and so can't run in parallel with any other thread.

Trevor Tippins
A: 

A glib (but realistic answer) for the maximum is that they might take an infinite amount of time to complete, as multi-threaded programs often contain deadlock bugs.

Daniel Earwicker
A: 

It depends! There isn't enough information to quantify this.

Missing Info: Hardware - How many threads can run at the same time on your CPU. Workload - Does it take 5 minutes because it's doing something for 5 minutes or is it performing some calculation that usually takes about 5 min's and uses a lot of CPU resources.

When you run multiple threads concurrently there can be lock waits for resources or the threads may even have to take turns executing and although they have been running for 5 minuets they may only have had a few CPU seconds.

5 threads never euqals 5X output. It can get close but will never reach 5X.

Nate Zaugg
A: 

I am not sure whether you are looking for CPU time spent by the thread. If that is the case, you can measure the CPU time, see below

ThreadMXBean tb = ManagementFactory.getThreadMXBean()
long startTime= tb.getCurrentThreadCpuTime();

Call the above when thread is created

long endTime= tb.getCurrentThreadCpuTime();

The difference between endTime - starTime, is the CPU time that the thread used

webjockey