dcsimg
Having several threads fill a collection
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   shahar_shalev
Posted On:   Saturday, May 5, 2007 06:05 AM

Hey,



I would highly appreciate a (short) sample code for filling an array by multiple threads. The program should use several (say 3) threads to fill a collection of ints (an int array would do here). To simplify things, each thread may use a constant int value in its turn. Of course, each thread should not overrun the input of another.
I am working on several possible solutions. I would really like to see a (simple) reference sample too.



Thanks in advance

Re: Having several threads fill a collection

Posted By:   WarnerJan_Veldhuis  
Posted On:   Saturday, May 5, 2007 08:19 AM

make the the adding of the value synchronized:

public synchronized addValue(int value) {
theCollection.add(value);
}

You can also work with a synchronized List:

List theCollection = Collections.synchronizedList(new ArrayList());
About | Sitemap | Contact