Synchronized method or synchronized block?
同步方法或同步代码块?
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()方法返回实例。
The compiler is allowed to move statements into a synchronized block — just not out of it.
编译器可以把语句移入synchronized块——而不仅仅是把语句移出。
Lock coarsening occurs when adjacent synchronized blocks may be merged into one synchronized block.
当多个彼此靠近的同步块可以合并到一起,形成一个同步块的时候,就会进行锁粗化。
When one thread is executing a synchronized block, any thread waiting to enter that block is stalled.
当一个线程正在执行一个同步块时,任何等待进入该块的线程都将被阻塞。
Then, one thread enters the synchronized block to initialize instance, while the other is blocked.
然后,一个线程进入synchronized块来初始化instance,而另一个线程则被阻断。
Why isn't calling wait(), notify() or notifyAll() without a synchronized block not a compiler error?
为什么不打电话给wait(),没有一个同步块不是一个编译错误notify()或notifyall()?
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。
When the first thread exits the synchronized block, the waiting thread enters and creates another Singleton object.
当第一个线程退出synchronized块时,等待着的线程进入并创建另一个Singleton对象。
When the thread exits the synchronized block of code, the lock is released, no matter how the thread exits the block.
当执行绪结束程式码的同步化区块时,锁定会被释放,不管执行绪如何结束该区块。
Do code path analysis to see if there is a synchronized block in the code that is blocking requests to execute concurrently.
进行代码路径分析,以查看阻塞请求并发执行的代码中是否存在同步阻塞。
It tries to do this by introducing the local variable inst and a second synchronized block. The theory works as follows.
它试图通过引入局部变量inst和第二个synchronized块来解决这一问题。
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
有一些并发缺陷有时不会使代码出错,但是它们可能会降低应用程序的性能。
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.
根据我们的观察,我们似乎需要对一些锁的访问进行优化,比如线程执行的同步块代码在一个循环体中。
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"操作。
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要求本地处理器缓冲刷新到主存中。
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.
要是您想用一小块线程安全代码把要求同步的两个操作隔开,那么只使用一个同步块一般会更好些。
Similarly, attempting to acquire an intrinsic lock (enter a synchronized block) cannot be interrupted, but ReentrantLock supports an interruptible acquisition mode.
类似地,尝试获取一个内部锁的操作(进入一个synchronized块)是不能被中断的,但是ReentrantLock支持可中断的获取模式。
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.
如果同步代码块不够小,应该对代码进行分析,对其重构,以使所有可以异步运行的代码均位于同步代码块之外。
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中,声明任务的方法是用一个查询表,并结合早期用于消除同步块需求的查询和声明方法。
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).
它也确保了编译器不会把指令从一个同步块的内部移到外部(虽然在某些情况下它会把指令从同步块的外部移到内部)。
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关键字强制实施一个互斥锁(互相排斥),这个互斥锁防止每次有多个线程进入一个给定监控器所保护的同步语句块。
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块关联的锁是一个线程本地变量。
应用推荐