Hi Alexander,
Thanks for your time and effort.
In fact my aspect is enough for my use case. I think I didn’t clearly explain my scenario. Here is some log printed on a simple thrift server with my aspect:
[Created Runnable] PID=27961@localhost, TID=1, org.apache.thrift.server.TThreadPoolServer$WorkerProcess(instanceID=0, creatorTID=1)
[Submit Runnable] Executor PID=27961@localhost, TID=1, org.apache.thrift.server.TThreadPoolServer$WorkerProcess(instanceID=0, creatorTID=1)
[Running Runnable/Callable] PID=27961@localhost, TID=10, org.apache.thrift.server.TThreadPoolServer$WorkerProcess(instanceID=0, creatorTID=1)
[Finished Runnable/Callable] PID=27961@localhost, TID=10, org.apache.thrift.server.TThreadPoolServer$WorkerProcess(instanceID=0, creatorTID=1)
What I want to know:
- Thread TID=1 submitted a Runnable (instanceID=0, creatorTID=1) to an Executor.
- Thread TID=10 (which is some thread in the thread pool used by that Executor) started running that Runnable (instanceID=0, creatorTID=1).
And the problem you pointed out in your example - 2 Runnables on line 1 and line 5 has the same instanceID:
[Create Runnable/Callable] TID=1, de.scrum_master.app.SomeRunnable(instanceID=0, creatorTID=1)
[Create Runnable/Callable] TID=1, de.scrum_master.app.Application$InnerStaticRunnable(instanceID=0, creatorTID=1)
[Create Runnable/Callable] TID=1, de.scrum_master.app.Application$InnerRunnable(instanceID=0, creatorTID=1)
[Create Runnable/Callable] TID=1, de.scrum_master.app.SomeRunnable(instanceID=1, creatorTID=1)
[Create Runnable/Callable] TID=12, de.scrum_master.app.SomeRunnable(instanceID=0, creatorTID=12)
[Create Runnable/Callable] TID=1, de.scrum_master.app.RunnableBase(instanceID=0, creatorTID=1)
Duration (minus sleeping time) = 22 ms
Unexpected counter value for class de.scrum_master.app.SomeRunnable: expected=3, actual=2
Unexpected number of total IDs: expected=6, actual=5
This problem doesn’t matter because they have different creatorTIDs (SomeRunnable(instanceID=0, creatorTID=1) vs. SomeRunnable(instanceID=0, creatorTID=12)), so they can be differentiated from each other.
I understand that ThreadLocal doesn’t synchronize or lock anything, and that’s the reason I used it - to remove synchronization as much as possible. And it’s OK that some Runnables have the same instanceID, because they will definitely have different creator Thread IDs. Please feel free to tell me where I didn’t explain clearly and lost you.
Your fix for “extra synchronization needed in your method” is elegant! I’ll definitely try both solutions.
Again thanks for your time into this. I learnt a lot. Really appreciate it.
regards,
Yongle