Java vs. C#

synchronized vs. lock


Java
 
class Cache
{
    public void add(Object x) {
        synchronized (this) {
            ...
        }
    }
    public void remove(Object x) {
        synchronized (this) {
            ...
        }
    }
}

C#
 
class Cache
{
    public static void Add(object x) {
        lock (typeof(Cache)) {
            ...
        }
    }
    public static void Remove(object x) {
        lock (typeof(Cache)) {
            ...
        }
    }
}
or lock(this) {...}

javacamp.org would like to thank Mr. Kukwenko who points out that in Java you cannot access "this" from a static context. This reminds of readers that you cannot use equivalent static method to access "this".