Clark
Clark

Reputation: 2213

Multithreading performance

I have Java application which run threads to do some job for N times for each thread and there can be setuped threads number. Each job iteration for each thread takes from 20 seconds to 1-1.5 minutes. Each thread have to do about 25000-100000 iterations for that job. So previously added jobs have more "done jobs" and they have higher priority (as I thinks for JVM but prioritet isn't setuped and they have equal priority programmly). But I need threads to do jobs evenly after some new tasks addition. For example there are 5000 threads to do 100 jobs for 100000 iterations:

But when I add for example job #101 then I'll see that threads don't run it as fast as first jobs. I've used yield() and sleep(50) but it seems not good results. So can you please tell me what am I doing wrong and how to do excellent performance for too many threads?

Upvotes: 0

Views: 389

Answers (2)

JB Nizet
JB Nizet

Reputation: 692121

Threads are scheduled by the OS scheduler, and you can't expect them to execute in a fixed order like this. Only that each thread should have some allocated time from the scheduler. If the tasks are independant from the other ones, you shouldn't care about the order anyway. If they're not independant, then you should make them collaborate to make sure that everything is executed in the appropriate order.

Having 5000 threads is probably far too much. How many processors does your machine have? What are the tasks executed by the threads. If they're CPU-bound, your best bet is to have a number of threads equal to the number of processors, or to the number of processors + 1.

Upvotes: 2

Jesper
Jesper

Reputation: 206946

It's hard to tell you what you are doing wrong (if anything) because you didn't tell us anything about how you exactly implemented this.

You might want to use an ExecutorService (thread pool) that can automatically distribute jobs for you over a number of threads. See Executor Interfaces from Oracle's Java Tutorials.

You didn't tell us anything about what the program is doing, but maybe the Java 7 Fork/Join framework might be useful, if the problem is suited for that.

Upvotes: 1

Related Questions