Synchronization

The basic synchronization support in .NET is similar to that in Java. For advanced functionality, .NET goes into areas not covered by Java, exposing some sophisticated tools and fine-grain controls.

Basic Locking

In Java, the keyword synchronized is used to ensure that only one thread will enter a specific section of code at a time. In the following example, Java ensures that a thread will not enter the code block containing the comment until an exclusive lock has been obtained on the this object.

public void doSomething() {
    synchronized (this) {
        // some synchronized operation
    }
}

When the thread exits the code section, Java will automatically release the lock so that another thread can attempt to acquire it and enter the synchronized ...

Get C# for Java Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.