17.3. Thread synchronization

Of course multithreading comes with a whole host of synchronization problems. There are concurrency problems which lead to indefinite states, [8] and other complicated 'programming traps' such as race conditions and deadlocks. Hence, it is important for a C# developer using threads to be well versed in thread synchronization.

[8] For example, if we have two concurrently running threads getting and setting the value of a shared variable, the exact value stored in this variable will be indefinite at any one time.

In this section, I will introduce the lock keyword, and two more classes – Mutex and Monitor.

Table 17.3 gives some initial clues as to how thread synchronization compares in C# and Java.

17.3.1. Thread safety ...

Get From Java to C#: A Developer's Guide 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.