Synchronized methods and blocks allow a thread to lock an object on entry and unlock the object on exit (to the method or block).
同步的方法和代码块允许线程在入口处锁定对象并在出口处解锁(针对方法或代码块)。
If you have two operations that require synchronization separated by a small block of thread-safe code, you are generally better just using a single synchronized block.
要是您想用一小块线程安全代码把要求同步的两个操作隔开,那么只使用一个同步块一般会更好些。
When one thread is executing a synchronized block, any thread waiting to enter that block is stalled.
当一个线程正在执行一个同步块时,任何等待进入该块的线程都将被阻塞。
Under those conditions the values in the synchronized block will never be contended for.
在这种条件下,同步块中的值永远不会存在竞争。
Similarly, as part of acquiring the monitor when entering a synchronized block, local caches are invalidated so that subsequent reads will go directly to main memory and not the local cache.
与此类似,作为获得监视的一部分,当进入一个同步块时,本地缓存失效,使之后的读操作直接进入主内存而不是本地缓存。
In cases where synchronization is necessary, the synchronized code block should be minimized.
在有必要进行同步的场合下,应该尽量缩小同步代码块。
Creating the synchronized block yielded 16 lines of bytecode, whereas synchronizing the method returned just 5.
创建同步代码块产生了16行的字节码,而创建同步方法仅产生了5行。
These locks do not require block structures, so they are more flexible than synchronized methods or statements.
这些锁不需要使用语句结构,所以它们比同步方法或语句更灵活。
The percentage performance penalty implied by this fixed delay depends on how much work is being done in the synchronized block.
而这一固定的延迟带来的性能损失百分比取决于在该同步块内做了多少工作。
The longer a synchronized code block requires for execution, the longer other threads wait to enter that block.
同步代码块所需的执行时间越长,其他线程等待进入该代码块的时间就越长。
But if the hashes differ, the out-of-date block is marked as out of sync, and subsequent synchronization ensures that the block is properly synchronized.
如果散列值不相同,将过期的数据块标记为不同步,随后的同步确保数据块是正确同步的。
The reason is that the synchronized block in Listing 5 prevents parallel execution.
其原因是清单5的同步代码阻止了并行执行。
If it is not small enough, you should analyze your code and refactor it in such a way that anything that could run asynchronously is located outside of the synchronized block.
如果同步代码块不够小,应该对代码进行分析,对其重构,以使所有可以异步运行的代码均位于同步代码块之外。
When a thread exits a synchronized block as part of releasing the associated monitor, the JMM requires that the local processor cache be flushed to main memory.
当线程为释放相关监视器而退出一个同步块时,J MM要求本地处理器缓冲刷新到主存中。
In Listing 6, the loop is out of a synchronized block, so it can be executed by several threads in parallel.
在清单6中,循环被移出同步语句,所以它可能由多个线程并行执行。
Do code path analysis to see if there is a synchronized block in the code that is blocking requests to execute concurrently.
进行代码路径分析,以查看阻塞请求并发执行的代码中是否存在同步阻塞。
In light of our observation it seems as if we should be able to optimize access in the case where a thread is looping over a synchronized block of code.
根据我们的观察,我们似乎需要对一些锁的访问进行优化,比如线程执行的同步块代码在一个循环体中。
This synchronized code block acquires the lock associated with the customer class object before it executes.
同步的代码块在执行之前需要与客户类对象相关的锁。
Also, each Home instance is cached once found, so that subsequent lookups will obtain it from the cache, and this cache access is protected with a synchronized block.
而且,一旦找到缓存的主机实例,随后的查找都可以从缓存中获取,这种缓存访问受到同步块的保护。
Lock coarsening occurs when adjacent synchronized blocks may be merged into one synchronized block.
当多个彼此靠近的同步块可以合并到一起,形成一个同步块的时候,就会进行锁粗化。
In WebSphere Process Server V7, the method to claim a task USES a query table and combines the query and claim methods previously used to eliminate the need for the synchronized block.
在WebSphereProcessServerv 7中,声明任务的方法是用一个查询表,并结合早期用于消除同步块需求的查询和声明方法。
And, if this process is applied repeatedly, the entire loop can be collapsed into a single synchronized block with a single "counter=10000000" operation.
而且,如果重复应用这个过程,整个循环将缩水成一个单独的同步块,这个同步块中只有一个 "counter=10000000"操作。
It also ensures that the compiler does not move instructions from inside a synchronized block to outside (although it can in some cases move instructions from outside a synchronized block inside).
它也确保了编译器不会把指令从一个同步块的内部移到外部(虽然在某些情况下它会把指令从同步块的外部移到内部)。
Multiple instances can be created even if you add a synchronized (this) block to the constructor call, as in Listing 4.
即使你为构造方法调用加上同步控制,多个实例的现象也会出现,见清单4。
Most programmers know that the synchronized keyword enforces a mutex (mutual exclusion) that prevents more than one thread at a time from entering a synchronized block protected by a given monitor.
大多数程序员都知道,synchronized关键字强制实施一个互斥锁(互相排斥),这个互斥锁防止每次有多个线程进入一个给定监控器所保护的同步语句块。
It is crucial to accurately identify which code block truly needs to be synchronized and to synchronize as little as possible.
准确地识别哪些代码真正需要同步并尽可能少地进行同步,这非常重要。
Then, one thread enters the synchronized block to initialize instance, while the other is blocked.
然后,一个线程进入synchronized块来初始化instance,而另一个线程则被阻断。
When the first thread exits the synchronized block, the waiting thread enters and creates another Singleton object.
当第一个线程退出synchronized块时,等待着的线程进入并创建另一个Singleton对象。
While no one would ever directly use the idiom in Listing 1, this code is very similar to the case where the lock associated with a synchronized block can be proven to be a thread-local variable.
虽然没有人会直接使用清单1中的形式,但是与这个代码非常类似的情况是:可以证实与synchronized块关联的锁是一个线程本地变量。
The compiler is allowed to move statements into a synchronized block — just not out of it.
编译器可以把语句移入synchronized块——而不仅仅是把语句移出。
应用推荐