什么情況下需要Java的Synchronized?

Multi-threaded programs may often come to a situation(情況) where multiple threads try to access the same resources and finally produce erroneous and unforeseen results.
So it needs to be made sure by some synchronization method that only one thread can access the resource at a given point of time.
? ? ? ?在使用多線程程序時(shí) 可能經(jīng)常會(huì)遇到多個(gè)線程嘗試訪問(wèn)相同資源并最終產(chǎn)生錯(cuò)誤和無(wú)法預(yù)料的結(jié)果的情況。
? ? ? ? 因此,多線程程序需要通過(guò)某種同步方法來(lái)確保在給定的時(shí)間點(diǎn)只有一個(gè)線程可以訪問(wèn)資源。
Java provides a way of creating threads and synchronizing their task by using synchronized blocks.
????????Java提供了一種使用同步塊創(chuàng)建線程并同步其任務(wù)的方法。
Synchronized blocks in Java are marked with the synchronized keyword. A synchronized block in Java is synchronized on some object.
All synchronized blocks synchronized on the same object can only have one thread executing inside them at a time.?
All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.
????????Java中的已同步塊都用sync關(guān)鍵字標(biāo)記。 Java中的同步塊在某些對(duì)象上同步。 在同一對(duì)象上同步的所有同步塊一次只能在其中一個(gè)線程內(nèi)執(zhí)行。????????
????????嘗試進(jìn)入同步塊的所有其他線程將被阻塞,直到同步塊內(nèi)的線程退出該塊為止。
This synchronization is implemented in Java with a concept called monitors.
Only one thread can own a monitor at a given time. When a thread acquires a lock, it is said to have entered the monitor.
All other threads attempting to enter the locked monitor will be suspended until the first thread exits the monitor.
????????此同步是在Java中通過(guò)一個(gè)稱為監(jiān)視器的概念實(shí)現(xiàn)的。
????????在給定的時(shí)間,只有一個(gè)線程可以擁有一個(gè)監(jiān)視器。 當(dāng)線程獲取鎖時(shí),據(jù)說(shuō)它已進(jìn)入監(jiān)視器。?
????????試圖進(jìn)入鎖定監(jiān)視器的所有其他線程將被掛起,直到第一個(gè)線程退出監(jiān)視器。
Following is an example of multi threading with synchronized.
????????以下是多線程使用Syn的案例
Output:
Code
The output is same every-time we run the program.每次我們運(yùn)行程序時(shí),輸出都是相同的。意思就是同步代碼塊起到作用了
In the above example, we chose to synchronize the Sender object inside the run() method of the ThreadedSend class. Alternately, we could define the whole send() block as synchronized and it would produce the same result.
在上面的示例中,我們選擇在ThreadedSend類的run()方法內(nèi)同步Sender對(duì)象。 或者,我們也可以將整個(gè)send()塊定義為已同步,并且將產(chǎn)生相同的結(jié)果。
Then we don’t have to synchronize the Message object inside the run() method in ThreadedSend class.
然后,我們不必在ThreadedSend類的run()方法內(nèi)同步Message對(duì)象。
We do not always have to synchronize a whole method. Sometimes it is preferable to synchronize only part of a method. Java synchronized blocks inside methods makes this possible.
我們并不總是必須同步整個(gè)方法。 有時(shí)最好只同步一部分方法。 同步方法內(nèi)部的一些方法塊更加行之有效 。
This article is contributed by Souradeep Barua. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now.
本文由Souradeep Barua提供。 如果發(fā)現(xiàn)任何不正確的地方,或者想分享有關(guān)上述主題的更多信息,請(qǐng)發(fā)表評(píng)論。
讀者注意! 請(qǐng)不要停止學(xué)習(xí)