How many threads do I use ?

Hi,

I have a program that has two types of threads:

1) Reader threads

2) Worker Threads

Readers: Their only job is to read files. They just read data from the files and put them into a buffer. They are obviously I/O intensive.

Workers: These are CPU intensive. They do some computation on the data which has been put into these buffers.

How many threads:

1) Worker: I think this answer is obvious. The number of worker threads should be equal to the number of CPUs in the sytem.

2) Reader: What about these? How can I determine the capability of I/O system ?

Specifically, I want the user to be able to specify the number of Reader and Worker threads the would like the program to use.What guidelines should the user follow or use to determing this number ? How can they determine the capabilities of the I/O subsystem? (The program uses 1 reader and 4 worker threads by default)

[963 byte] By [the_learnera] at [2007-11-27 10:40:08]
# 1

I don't think you can really make a general rule, for example IO capacity can depend on how the files being processed are spread over disc drives. Reading files from two different drives can give you increased capacity compared to reading, for example, two files which are on different parts of the same drive.

What you're doing sound like a case for a ThreadPoolExecutor, creating a pool of worker threads and feeding them from a queue of read data.

In that environment a few extra threads aren't likely to do any harm.

In summary - experimentation is your best option.

malcolmmca at 2007-7-28 19:05:06 > top of Java-index,Java Essentials,Java Programming...
# 2

> 1) Worker: I think this answer is obvious. The number

> of worker threads should be equal to the number of

> CPUs in the sytem.

If CPU cores are the only limiting factor, yes. If one thread at some point has to wait for a resource to become available, then in the meantime, another one could do its own processing. So it's not like multi-threading doesn't make sense on a single-core CPU engine.

> 2) Reader: What about these? How can I determine the

> capability of I/O system ?

If all the files are on devices handled by the same controller: one. Concurrent access to hard disks would only slow things down as you'd bypass the caching and sequential-read mechanisms by practically requesting random data chunks all the time.

CeciNEstPasUnProgrammeura at 2007-7-28 19:05:06 > top of Java-index,Java Essentials,Java Programming...