O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required