The reason is that the synchronized block in Listing 5 prevents parallel execution.
其原因是清单5的同步代码阻止了并行执行。
Under those conditions the values in the synchronized block will never be contended for.
在这种条件下,同步块中的值永远不会存在竞争。
Thread 1 exits the synchronized block and returns instance from the getInstance() method.
线程1退出synchronized块并从getInstance()方法返回实例。
Lock coarsening occurs when adjacent synchronized blocks may be merged into one synchronized block.
当多个彼此靠近的同步块可以合并到一起,形成一个同步块的时候,就会进行锁粗化。
Then, one thread enters the synchronized block to initialize instance, while the other is blocked.
然后,一个线程进入synchronized块来初始化instance,而另一个线程则被阻断。
When one thread is executing a synchronized block, any thread waiting to enter that block is stalled.
当一个线程正在执行一个同步块时,任何等待进入该块的线程都将被阻塞。
In Listing 6, the loop is out of a synchronized block, so it can be executed by several threads in parallel.
在清单6中,循环被移出同步语句,所以它可能由多个线程并行执行。
Creating the synchronized block yielded 16 lines of bytecode, whereas synchronizing the method returned just 5.
创建同步代码块产生了16行的字节码,而创建同步方法仅产生了5行。
Note that when the second thread enters the synchronized block, it does not check to see if instance is non-null.
注意:当第二个线程进入synchronized块时,它并没有检查instance是否非null。
It tries to do this by introducing the local variable inst and a second synchronized block. The theory works as follows.
它试图通过引入局部变量inst和第二个synchronized块来解决这一问题。
Do code path analysis to see if there is a synchronized block in the code that is blocking requests to execute concurrently.
进行代码路径分析,以查看阻塞请求并发执行的代码中是否存在同步阻塞。
The percentage performance penalty implied by this fixed delay depends on how much work is being done in the synchronized block.
而这一固定的延迟带来的性能损失百分比取决于在该同步块内做了多少工作。
Some concurrency bugs won't break your code, but they can lead to poor application performance. Consider the synchronized block in Listing 5
有一些并发缺陷有时不会使代码出错,但是它们可能会降低应用程序的性能。
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"操作。
Similarly, attempting to acquire an intrinsic lock (enter a synchronized block) cannot be interrupted, but ReentrantLock supports an interruptible acquisition mode.
类似地,尝试获取一个内部锁的操作(进入一个synchronized块)是不能被中断的,但是ReentrantLock支持可中断的获取模式。
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.
而且,一旦找到缓存的主机实例,随后的查找都可以从缓存中获取,这种缓存访问受到同步块的保护。
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.
要是您想用一小块线程安全代码把要求同步的两个操作隔开,那么只使用一个同步块一般会更好些。
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.
如果同步代码块不够小,应该对代码进行分析,对其重构,以使所有可以异步运行的代码均位于同步代码块之外。
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.
与此类似,作为获得监视的一部分,当进入一个同步块时,本地缓存失效,使之后的读操作直接进入主内存而不是本地缓存。
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).
它也确保了编译器不会把指令从一个同步块的内部移到外部(虽然在某些情况下它会把指令从同步块的外部移到内部)。
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块关联的锁是一个线程本地变量。
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关键字强制实施一个互斥锁(互相排斥),这个互斥锁防止每次有多个线程进入一个给定监控器所保护的同步语句块。
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关键字强制实施一个互斥锁(互相排斥),这个互斥锁防止每次有多个线程进入一个给定监控器所保护的同步语句块。
应用推荐