class ThreadSafeClass extends Thread
{
private static int count = 0;
public synchronized static void increment()
{
count++;
}
public
decrement
is locking on a different thing to increment
so they do not prevent each other from running.decrement
on one instance is locking on a different thing to calling decrement
on another instance, but they are affecting the same thing.The first means that overlapping calls to increment
and decrement
could result in a cancel-out (correct), an increment or a decrement.
The second means that two overlapping calls to decrement
on different instances could result in a double decrement (correct) or a single decrement.