@synchronized()是什么? [英] What does @synchronized() do?
问题描述
我刚刚创建了一个singleton方法,我想知道什么函数 @synchronized()
,因为我经常使用它,但不知道
I just created a singleton method, and I would like to know what the function @synchronized()
does, as I use it frequently, but do not know the meaning.
推荐答案
它声明一个关键部分围绕代码块。在多线程代码中, @synchronized
保证在任何给定时间只有一个线程可以在块中执行该代码。
It declares a critical section around the code block. In multithreaded code, @synchronized
guarantees that only one thread can be executing that code in the block at any given time.
如果你不知道它是什么,那么你的应用程序可能不是多线程的,你可能不需要使用它(特别是如果单例本身不是线程安全的)。
If you aren't aware of what it does, then your application probably isn't multithreaded, and you probably don't need to use it (especially if the singleton itself isn't thread-safe).
这篇关于@synchronized()是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!