I have seen question How do determine if an object is locked (synchronized) so not to block in Java?
But I have problem on which I can not figure out solution.
In my web application is process in which refresh of data container and that can take long time. Refresh is forced to be made in time intervals. Of course when one refresh is still on container working another cannot (to do not corrupt data in container).
I want to use background thread to refresh container. Multiple background workers can be working at the same time on multiple containers (different workers for different user sessions, every user session can have different data in their container).
Of course I can do synchronize(myContainer)
in worker to force that any other worker is not currently updating this particular container. But I rather want to check if any worker is working on container and exit, if is. Also I would like to not change code of container, so I would like not to add ReentrantLock inside container class and lock on that.
So worker have MyContainer
instance and want to determine if any other worker is currently refreshing this container instance.
Any ideas how to achieve that?