Java synchronized statement is a way of protecting a data resource from being access simultaneously by multiple threads. It can be done via doing synchronize to an object X as a critical section or mutex.
class MyClass
{
int a;
object X = new object();
void Foo()
{
synchronized(X)
{
a++;
}
}
}
Is there any way of doing try-lock to X instead of lock like this?
void Foo()
{
if(try_synchronized(X)==true)
{
a++;
}
}
Unfortunately, this Q&A say that ReentrantLock is the alternative way, but synchronized keyword is implemented under Java VM, so this question is different.