3

I'm experiencing some strange behaviour in a java program. Basically, I have a list of items to process, which I can choose to process one at a time, or all at once (which means 3-4 at a time). Each item needs about 10 threads to be processed, so processing 1 item at a time = 10 threads, 2 at a time = 20 threads, 4 at a time = 40 threads, etc.

Here's the strange thing, if I process just one item, its done in approx 50-150 milliseconds. But if I process 2 at a time, it goes up to 200-300 ms per item. 3 at a time = 300-500MS per item, 4 at a time = 400-700 MS per item, etc.

Why is this happening? I've done prior research which says that jvm can handle upto 3000-4000 threads easily, so why does it slow down with just 30-40 threads for me? Is this normal behavior? I thought that having 40 threads would mean each thread would work in parallel rather than in a queue as it seems to be.

7
  • Your machine has less processors/cores than the number of threads you start, an the overhead of context switching between the tons of threads you have has a significant overhead Commented Oct 3, 2013 at 11:08
  • are you using synchronized methods? The reason may be "collision" between threads, since synchronized methods in fact slow down performance. Give each thread its own methods/parameters and see what happens Commented Oct 3, 2013 at 11:10
  • @AsierAranbarri: even if he/she is not, the negative impact would be remarkable nevertheless. He just needs to basically serialize the items processing (one at a time). Parallel != better. Commented Oct 3, 2013 at 11:11
  • @SimonTodd What's the advantage of using a threadpool ? Commented Oct 3, 2013 at 12:15
  • @gd1 the overhead is only significant if the threads are CPU-intensive AND use so much data that a large amount of L1 cache has to be swapped out upon context-change. Commented Oct 3, 2013 at 12:36

2 Answers 2

3

How many CPU cores do you have?

If I have one CPU core, and I max out a single threaded application on it, the CPU is always busy, if I give it two threads, both doing this heavy task I don't get double-the-cpu, no, they both get ~0.5 seconds / second (seconds per second) of CPU time take away the time the OS needs to switch threads.

So it doubles the time taken for each thread to work, but they might finish at about the same time (depending on the scheduler)

If you have two CPU cores.... then it'd (theoretically again) finish in the same time as one thread, because one thread can't use two cpu cores (at the same time)

Then there's hardware threads, some threads yield or sleep, if they're reading/writing the OS will run other threads while they are blocked, so forth....

Does this help?

Sign up to request clarification or add additional context in comments.

3 Comments

I'm on Intel core 2 DUO, I think it has two cores, 2.26 Ghz and 2.27 Ghz
No, I see your point. I just mentioned that in case you wanted to know which CPU I had.
@user2838678 tick and upvote when ready? Unless I missed something :P
0

It would be nice to see some source code. Without it i have only 4 assumptions :

1) You haven't done the load balancing. You should consider about optimal number of threads.

2) Work, executed by each thread does not justify the time, needed to setup and start the thread (+ context switching time).

3) There is the real problems with your code quality

4) Weak hardware

5 Comments

I think this is a question best answered by "go read about what a thread is" I get the impression the OP thought they were magic, if it were slow code he would have surely at least described it.
@AlecTeal multithreading, are you a wizard? 1-media-cdn.foolz.us/ffuuka/board/a/image/1354/83/…
@AlecTeal what does "OP" mean ? Never seen it before :)
@oleg.lukyrych "original poster", the person who starts a thread or in this case asks a question.
@AsierAranbarri I like to think so, but demonstrating it on this question would be silly, my answer was only just above "imagine a pregnant woman, Ms C. P. U. Core, she's pregnant with an unknown number of children, she plans to call them Hard, Pog, Ware and Thread..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.