1

I am observing a very strange problem in a java client server application. I am sending following Runnable objects to the server at 80 requests per second. The thread pool keeps pool size equal to the request rate i.e. approximately 80 threads in the pool. My laptop is intel Core i5-3230M dual core(Windows show me 4 processor). Strange thing is that the Throughput(jos completed per second) is also 80. I could not understand this. How 4 processors and 80 threads are completing 80 jobs of 100 milliseconds in one second ? That is:

Processors=4
Request rate=80
Thread pool size=80
Each job service time=100milliseconds.
Throughput=80 How?

I was expecting throughput=40 because each processor should approximately complete 10 jobs in 1 second so 4 processors should give throughput=40 but it is 80 ? Laptop specification link says

Also, the cores can handle up to four simultaneous threads, which improves the performance and resource-utilization of the CPU.

Does this means 8 threads can run at the same time b 2 cores?

public class CpuBoundJob  implements Runnable {

    public void run() {

     long startTime = System.nanoTime();
         while ((System.nanoTime() - startTime) < (100)*1000000L) {}

    }
}
Faisal Bahadur
  • 498
  • 3
  • 19

1 Answers1

3

You have written tasks which run for a fixed amount of time, not a fix amount of work. This means they should always complete at a fixed rate regardless of the number of CPUs you have. You could just have them sleep for 100 ms.

How 4 processors and 80 threads are completing 80 jobs of 100 milliseconds in one second ?

Your computer is running far more threads than you have processes all the time. The OS uses scheduling to stop and start running thread (faster than you can see) to give the illusion they all running at once but they are not and cannot (never could if you think about it)

Also, the cores can handle up to four simultaneous threads, which improves the performance and resource-utilization of the CPU.

Its means it's two cores have hyper threading allowing the processor to running up to four threads without context switching (as mentioned above)

Does this means 8 threads can run at the same time b 2 cores?

The i5 mentioned has 2 cores it supports 4 threads as it states.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • You said "You have written tasks which run for a fixed amount of time, not a fix amount of work. This means they should always complete at a fixed rate regardless of the number of CPUs you have." I could not understand this. It means a single cpu system will give the same throughput? – Faisal Bahadur Jan 10 '16 at 16:43
  • @FaisalBahadur correct. I guess I have been working with computers too long, but I don't understand what you would expect to happen which is different. – Peter Lawrey Jan 10 '16 at 16:45
  • @FaisalBahadur Instead I would expect the question to be; why is it that when I increase the number of tasks I see it takes slightly longer. i.e. it does matter a little bit how many threads you read, especially if you busy wait instead of sleeping. – Peter Lawrey Jan 10 '16 at 16:46
  • Ok if i5 supports 4 threads at the same time then in 1 second 4 threads can run 40 runnable objects of 100ms. Is this not correct. plz sir help me understand this i am so much confused – Faisal Bahadur Jan 10 '16 at 16:48
  • 4 threads at one time doesn't mean that within say 100 ms second only 4 thread ever run. Your operating system supports context switching so these threads will be interrupted to let other threads run. The minimum time slice is typically 100 micro-seconds, so your processor could run up to 4000 threads in 100 ms. – Peter Lawrey Jan 10 '16 at 16:53
  • @FaisalBahadur https://en.wikipedia.org/wiki/Context_switch "context switch ... is an essential feature of a multitasking operating system." – Peter Lawrey Jan 10 '16 at 16:53
  • Actually sleep was not run for accurate time that is why i used spin. I used spin for cpu-ound request and sleep for i/o bound request for simulation. but sleep is so problematic regarding accuracy. But spin is perfect. – Faisal Bahadur Jan 10 '16 at 16:57
  • @FaisalBahadur sleep is not 100% accurate but if you specify 100 ms it is likely to be an arbitrary choice in any case. Note: ScheduledExecutorService use LockSupport.parkNanos and corrects for any drift to the error doesn't accumulate. – Peter Lawrey Jan 10 '16 at 17:01
  • @FaisalBahadur Your task isn't useful to simulate a CPU process as it will do less work if the thread doesn't get a chance to run. You need to replace it with a fixed amount of work the JIT can't optimise away. – Peter Lawrey Jan 10 '16 at 17:02
  • @FaisalBahadur write some code which does something a fixed number of times. – Peter Lawrey Jan 10 '16 at 18:42
  • You mean i should follow the question in following link? plz help? http://stackoverflow.com/questions/3693197/cpu-intensive-calculation-examples – Faisal Bahadur Jan 10 '16 at 18:51
  • @FaisalBahadur it doesn't have to be so complicated, just anything which the JIT can't optimise away to nothing. – Peter Lawrey Jan 10 '16 at 18:54
  • @FaisalBahadur On my system this takes about 100 ms `int n = IntStream.range(0, 80000000).max().orElse(0);` You can adjust the number to taste. – Peter Lawrey Jan 10 '16 at 19:01
  • Peter Lawrey. Your suggestions helped me alot! So nice of you . plz one last thing i want to ask you if u are here! – Faisal Bahadur Jan 12 '16 at 08:45
  • @FaisalBahadur if you have another question, I suggestion you open a new question. – Peter Lawrey Jan 12 '16 at 11:13
  • I have opend my question. plz check Question title "Service times directly propotional to number of threads". Link "http://stackoverflow.com/questions/34748495/service-times-directly-propotional-to-number-of-threads?noredirect=1#comment57244321_34748495" – Faisal Bahadur Jan 12 '16 at 16:59
  • plz help! as nobody is answering my new question – Faisal Bahadur Jan 12 '16 at 17:07
  • @FaisalBahadur You just need to write any code which actually uses the CPU and runs for the same amount of time. There is no specific answer to your question and almost any code will do this (looping on a timer is an exception) – Peter Lawrey Jan 12 '16 at 20:38