16.4. Thread Synchronization

Certain programming instructions are guaranteed to be never preempted by a thread X while another thread Y is running them. Such atomic operations have built-in thread safety. However, in OOP, operations (method calls) are seldom atomic, and the state of the object is rarely as simple as incrementing an integer.

Objects that are designed for thread safety do not allow multiple threads to simultaneously change their state. Such objects identify certain sections of the code as critical and put appropriate locks around that code so that only one thread can call that block of code at any one time. Running the block of code requires the caller thread to acquire a lock that prevents other threads from accessing that code. ...

Get .NET for Java Developers: Migrating to C# 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.