1

I am working on a multi-threaded application and need help with some pseudo-code. To make it simpler for implementation I will try to explain that in simple terms / test case.

Here is the scenario -

I have an array list of strings (say 100 strings)

I have a Reader Class that reads the strings and passes them to a Writer Class that prints the strings to the console. Right now this runs in a Single Thread Model.

I wanted to make this multi-threaded but with the following features -

Ability to set MAX_READERS

Ability to set MAX_WRITERS

Ability to set BATCH_SIZE

So basically the code should instantiate those many Readers and Writers and do the work in parallel.

Any pseudo code will really be helpful to keep me going!

jagamot
  • 5,348
  • 18
  • 59
  • 96
  • Is the array given at start time, or is it a stream of incoming strings? – Yuval Apr 14 '10 at 19:07
  • Well, if you use stuff from `java.util.concurrent` it will be super easy... But since you want pseudo code, I guess you want it to implement without using it (for learning purposes?)? I'm not clear on why you have `MAX_WRITERS` AND `BATCH_SIZE` because if you do the writing in batch, the most obvious implementation would be to have a single writer always. (Waiting for multiple batch to accumulate and then doing those in multiple threads seems strange) – Enno Shioji Apr 14 '10 at 20:41

2 Answers2

3

This sounds like the classic consumer-producer problem. Have a look at Wikipedia's article about it. They have plenty of pseudo code there.

ilikeorangutans
  • 1,753
  • 1
  • 11
  • 14
0

Aside from using the producer-consumer pattern that has been suggested, I would recommend that you use the CopyOnWriteArrayList so you can have lock-free read/write/iteration of your list. Since you're only working with a couple of hundred strings you will probably not have any performance issues with the CopyOnWriteArrayList.

If you're concerned about performance then I actually think it might be better if you use the BlockingQueue or a ConcurrentHashMap. They will allow you to maximize throughput with your multithreaded application.


The recommended option:
A BlockingQueue works very well with multiple producers and consumers, but of course it implies an order of data processing (FIFO). If you're OK with FIFO ordering, then you will probably find that the BlockingQueue is a faster and more robust option.


I think that the Wikipedia article has sufficient pseudo code for you to use, but you can also check out some of the following SO questions:
https://stackoverflow.com/search?q=java+producer+consumer

Java Producer-Consumer Designs:
Producer/Consumer threads using a Queue
design of a Producer/Consumer app

Community
  • 1
  • 1
Kiril
  • 39,672
  • 31
  • 167
  • 226
  • @Lirik: Personally I find this very confusing. First, you normally don't want to use a CopyOnWriteArrayList in a typical producer/consumer model (if you produce a lot, it's not a good choice). Also I think it is unclear why a ConcurrentHashMap could be useful in this case. I mean, yes it performs good for a map, but where do you need a map? – Enno Shioji Apr 14 '10 at 22:34
  • @Zwei, the only reason I suggested the CopyOnWriteArrayList is because the OP is using an array and the OP stated that there will be around 100 strings in the array. Obviously BlockingQueue would be the better option for the Producer/Consumer design, so perhaps I should highlight that portion of my answer. There are many situations where a map is useful, here is one example: your producer is frequently updating a fixed number data fields and you only care for the latest updates (i.e. high frequency market data)... a queue will probably be a bad choice in that case. – Kiril Apr 14 '10 at 23:58
  • @ZWei - I stated in my post that I am using that particular example to make things understand better. The data that needs to be copied need not be strings...in general its actually database...so basically Readers would read sets of data from database and writers will persist them in a different database.. – jagamot Apr 15 '10 at 11:55
  • @HonorGod, I see. I still think though that List is generally a poor choice here.. I see that it is because the OP suggested, but I'd go as far as to say that it would be wrong to use a CopyOnWriteArrayList here. Especially because CopyOnWriteArrayList should only be used if you write rarely to the List. Since multiple writers are fetching data from the source, you normally want to de-queue things (for which Queue fits very well). This would be extremely cumbersome with List, and very inefficient with a CopyOnWriteArrayList.. Every time you remove/add, the entire List must be copied.. – Enno Shioji Apr 15 '10 at 15:28