PiaklA
PiaklA

Reputation: 505

How to Maintain single copy of List among st all threads in Java

class A implements Callable{

 List result     // want to share this List amongst thread and their function call

 public Search  call() {

 List<Job>  Jobs = api. Jobs(a,b,c);  // Multiple threads 

    }
 }


class API{
         public void jobs(a,b,c){
              // want to access the  A.Result and populate the result
         }
}     

How can i share an array List amogst all threds, I dont want to use the Static , as it will keep accumilating the result every time it runs , Is Thread Local is a good choice over here ?

Trying to avoid an extra object and its respective getters / setters ?

Upvotes: 0

Views: 133

Answers (2)

Eugene
Eugene

Reputation: 120968

I know you have your answer, but I did not understand the answer or the question, thus need to ask. So, you problem looks like this? You have a bunch of Callables that will perform some work on a SINGLE list, thus this list is shared among Threads? Then you need to make this List Thread Safe, and making a List of Objects Thread Safe is not that trivial (on your own), unless you use something already given by Java

Upvotes: 0

kosa
kosa

Reputation: 66657

What ever you have right now is thread shared list. All threads operating on this object (assuming only one instance of this object exists) share same list unless you synchronize.

Upvotes: 1

Related Questions