Reputation: 3
I have a list of brands and I want to set brand specific properties to each Object and run a thread of it.
However, when I run the below code... it creates multiple threads of same brand and leaves out few brands.
brightCoveVideoInfoPullerThread is a runnable class. In the object of this I am adding brand specific properties through BrightCoveAPIParam.
for (int i = 0; i < brands.size(); i++) {
String brand = brands.get(i);
brightCoveVideoInfoPullerThread.setBrightCoveAPIParam(properties.get(brand));
Thread t = new Thread(brightCoveVideoInfoPullerThread,
"BrightCovePullerThreadFor" + brand);
t.start();
}
e.g
Brightcove Poller for HEALTHCOM
Brightcove Poller for HEALTHCOM
Brightcove Poller for FOODANDWINE
Brightcove Poller for FOODANDWINE
Upvotes: 0
Views: 32
Reputation: 140319
You are reusing the same instance of brightCoveVideoInfoPullerThread
on every iteration of the loop. Changing a property of that instance using the setter will update the property for all threads, since all threads are running that same instance.
Create a new instance of it inside the loop, so that each thread has its own instance:
for (String brand : brands) {
BrightCoveVideoInfoPullerThread brightCoveVideoInfoPullerThread = new ...;
brightCoveVideoInfoPullerThread.setBrightCoveAPIParam(properties.get(brand));
Thread t = new Thread(brightCoveVideoInfoPullerThread, "BrightCovePullerThreadFor" + brand);
t.start();
}
Upvotes: 1