官术网_书友最值得收藏!

Threads interleaving – the need for synchronization

The way threads get scheduled to run is within the realm of the operating system. Factors such as the system load, the number of processes running at a time on the machine, make thread scheduling unpredictable. A good example is a seat in a movie hall.

Let's say that the movie that is playing is a big crowd-puller. We need to follow a protocol; we book a ticket by reserving a seat. The following diagram shows the rules that are based around the ticket booking system: 

What if the booking is erroneously given to two people at the same time? The result would be chaotic, as both would rightfully try to go and occupy the seat at the same time. 

There is a certain framework at work to make sure this situation does not happen in practice. The seat is shared among interested people and one person needs to book it in advance.

Likewise, threads need to lock a resource (that is, a shared, mutable data structure). The problem is with explicit locking. If the onus of correctly synchronizing is with the application, then someone, someday may forget to do it right and all hell will break loose.   

To illustrate the need for correct synchronization, the following diagram shows an integer variable shared between two threads:  

As shown in the preceding diagram, if the interleaving happens to be right, things may work as expected. Otherwise, we will have a lost update to deal with.

Instead, just like a movie ticket acting as a lock, every Java object has a lock. A thread acquires it, performs the state mutations, and unlocks. This entire sequence is a critical section. If your critical section needs to mutate a set of objects, you need to lock each one separately. 

Generally, the advice is to keep the critical section as small as possible. We will discuss the reason in the next section

主站蜘蛛池模板: 东平县| 祁门县| 西宁市| 从江县| 长岭县| 白山市| 安阳市| 龙州县| 永平县| 祁连县| 郴州市| 珠海市| 惠水县| 阳山县| 沾化县| 孟津县| 延寿县| 九江市| 怀宁县| 阿合奇县| 斗六市| 红原县| 灵石县| 花莲市| 邳州市| 阿坝县| 琼海市| 遵义县| 冕宁县| 巴塘县| 咸宁市| 尉氏县| 金寨县| 北票市| 昌平区| 勐海县| 习水县| 静乐县| 建湖县| 英德市| 黄平县|