How it is possible that both static and non static java synchronized method running in parallel while writing java synchronized code ?
public class Counter{
private static count = 0;
public static synchronized getCount()
{
return this.count;
}
public synchronized setCount(int count)
{
this.count = count;
}
}
I still don’t understand the question and I’m confused by your code example. You have static methods referencing “this.counter” yet “counter” is static.
In any event, to re-state some of the other answers, consider:
“synchronized” means two different things in each case. On classMethod, which is static, “synchronized” applies to the class object’s (Counter.class) monitor, while instanceMethod’s “synchronized” applies to the object instance’s (“this”) monitor.
As such, classMethod and instanceMethod will not lock each other. instanceMethod would block another non-static synchronized method, while classMethod would block other static synchronized methods.