Reputation: 5031
I m wondering when do we need to use the threadlocal variable?, I have a code that runs multiple threads, each one read some files on S3, I wish to keep track of how many lines read out of the files altogether, here is my code:
final AtomicInteger logLineCounter = new AtomicInteger(0);
for(File f : files) {
calls.add(_exec.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
readNWrite(f, logLineCounter);
return null;
}
}));
}
for (Future<Void> f : calls) {
try {
f.get();
} catch (Exception e) {
//
}
}
LOGGER.info("Total number of lines: " + logLineCounter);
...
private void readNWrite(File f, AtomicInteger counter) {
Iterator<Activity> it = _dataReader.read(file);
int lineCnt = 0;
if (it != null && it.hasNext()) {
while(it.hasNext()) {
lineCnt++;
// write to temp file here
}
counter.getAndAdd(lineCnt);
}
}
my question is do I need to make the lineCnt
in the readNWrite()
method to be threadlocal?
Upvotes: 1
Views: 150
Reputation: 33534
- A Thread
has a Stack
, Register
, and Program Counter
.
- lineCnt
is already into the ThreadLocal
.
- lineCnt
is a personal copy of the instance variable lineCnt for this thread, and its not visible to any other thread .
Upvotes: 1
Reputation: 16335
You dont need to make lineCnt a ThreadLocal
explicitly. lineCnt
is a local variable to the thread. It is not accessible by any other thread.
You can get more information about ThreadLocal here
ThreadLocal from javadoc
These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread
Upvotes: 1
Reputation: 328568
No you don't need to use ThreadLocal here - your code looks perfectly fine:
lineCnt
is a local variable which is therefore not shared across thread => it is thread safecounter.getAndAdd(lineCnt);
is an atomic and thread safe operationIf you are interested, there are several posts on SO about the use of ThreadLocal, such as this one.
Upvotes: 2
Reputation: 86754
lineCnt
is already "thread local" since it's on the stack. Use ThreadLocal
only when you need thread-local copies of instance member variables.
Upvotes: 2