欢迎光临散文网 会员登陆 & 注册

什么情况下需要Java的Synchronized?

2021-09-05 16:22 作者:CodeSnake  | 我要投稿


Java - Intrinsic Locks and Synchronization
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.

       在使用多线程程序时 可能经常会遇到多个线程尝试访问相同资源并最终产生错误和无法预料的结果的情况。

        因此,多线程程序需要通过某种同步方法来确保在给定的时间点只有一个线程可以访问资源。

Java provides a way of creating threads and synchronizing their task by using synchronized blocks.

        Java提供了一种使用同步块创建线程并同步其任务的方法。

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关键字标记。 Java中的同步块在某些对象上同步。 在同一对象上同步的所有同步块一次只能在其中一个线程内执行。        

        尝试进入同步块的所有其他线程将被阻塞,直到同步块内的线程退出该块为止。

Following is the general form of a synchronized block: 

以下是同步块的一般形式

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中通过一个称为监视器的概念实现的。

        在给定的时间,只有一个线程可以拥有一个监视器。 当线程获取锁时,据说它已进入监视器。 

        试图进入锁定监视器的所有其他线程将被挂起,直到第一个线程退出监视器。

Following is an example of multi threading with synchronized.

        以下是多线程使用Syn的案例

Output:

Code

The output is same every-time we run the program.每次我们运行程序时,输出都是相同的。意思就是同步代码块起到作用了

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()方法内同步Sender对象。 或者,我们也可以将整个send()块定义为已同步,并且将产生相同的结果。

Then we don’t have to synchronize the Message object inside the run() method in ThreadedSend class.

然后,我们不必在ThreadedSend类的run()方法内同步Message对象。

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.

我们并不总是必须同步整个方法。 有时最好只同步一部分方法。 同步方法内部的一些方法块更加行之有效 。

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提供。 如果发现任何不正确的地方,或者想分享有关上述主题的更多信息,请发表评论。

读者注意! 请不要停止学习


什么情况下需要Java的Synchronized?的评论 (共 条)

分享到微博请遵守国家法律