1. To avoid liveness and safety failures, never cede control to the client within a synchronized method or block. In other words, inside a synchronized region, do not invoke a method that is designed to be overridden, or one provided by a client in the form of a function object. The class has no knowledge of what the method does and has no control over it. Depending on what an alien method does, calling it from a synchronized region can cause exceptions, deadlocks, or data corruption. It is usually not too hard to fix this sort of problem by moving alien method invocations out of synchronized blocks.
2. CopyOnWriteArrayList is a variant of ArrayList in which all write operations are implemented by making a fresh copy of the entire underlying array. Because the internal array is never modified, iteration requires no locking and is very fast.
3. As a rule, you should do as little work as possible inside synchronized regions. Obtain the lock, examine the shared data, transform it as necessary, and drop the lock. If you must perform some time-consuming activity, find a way to move the activity out of the synchronized region.
4. In a multicore world, the real cost of excessive synchronization is not the CPU time spent obtaining locks; it is the lost opportunities for parallelism and the delays imposed by the need to ensure that every core has a consistent view of memory. Another hidden cost of oversynchronization is that it can limit the VM’s ability to optimize code execution.
5. You should make a mutable class thread-safe if it is intended for concurrent use and you can achieve significantly higher concurrency by synchronizing internally than you could by locking the entire object externally. Otherwise, don’t synchronize internally. Let the client synchronize externally where it is appropriate. When in doubt, do not synchronize your class, but document that it is not thread-safe.
6. If a method modifies a static field, you must synchronize access to this field, even if the method is typically used only by a single thread.
相关推荐
Item 79: Avoid excessive synchronization Item 80: Prefer executors, tasks, and streams to threads Item 81: Prefer concurrency utilities to wait and notify Item 82: Document thread safety Item 83: Use ...
解决ORA 700 [kskvmstatact excessive swapping observed]
Detection of Excessive Interrupt Disablement - a new feature which can detect a period of excessive interrupt disablement on a CPU, and create an error log record to report it. This allows you to know...
Excessive 是一个用 Python 开发的 CSS 压缩工具,可拖放任意多的文件并将这些文件合并到一个文件并即时压缩。 标签:Excessive
example.rs:6:1: 10:2 warning: Struct contains an excessive number (3) of bools (is_leaving_session, is_connecting, is_doing_stuff). Did you want to create a state machine?, #[warn(excessive_bool_...
the grid under: 1) excessive dc-link voltage 2) excessive ac currents and 3) loss of grid-voltage synchronization. In this paper, the control of single- and two-stage grid-connected VSIs in ...
前面聊的 最近在调试项目的亮屏速度,我们希望在按下power键后到亮屏这个...01-18 09:13:40.992 683 772 D SurfaceControl: Excessive delay in setPowerMode(): 743ms 01-18 09:13:45.304 683 772 D S
受影响的站点有14个,查询这14个站点近一周的历史告警,发现WUH-鸿汇城西-WEL基站12.27日出现过Sync Reference Excessive Wander告警,之后出现了Timing Sync Fault告警,表示此时该基站已处于GPS失步状态。
先来看一张简单的文档树很明显树的顶层节点是NodeA节点,接下来可以通过指定的合适节点移动到树中的任何点,结合以下的代码你可以更好的了解这棵树节点间的相互关系:NodeA.firstChild = NodeA1 NodeA.lastChild = ...
The extent to which nuclear energy can be a ...and that it should avoid excessive reliance on the business sector and should assume a key role for itself in enhancing trust in nuclear decision-making
本文图文讲解 遇到:Simulation is not running in real time due to excessive CPU load 的解决办法
过量土壤磷抑制真菌生长的机理,曹志平,,磷是一种即将耗竭的资源,但人们对此却没有充分的认识。另一方面,近几十年来磷在农业土壤中的积累速度正在加快,有可能将农业土
Fixed: ListBox code regression in wfxApplication.inc WM_ERASEBKGND handler that caused excessive visual flicker (ControlType.ListBox was accidently set to ControlType.ListView). Fixed: ListView now ...
School-based intervention for the modification of excessive absenteeism 84 A Student Team Approach to Teaching REFERENCE NOTES 1. CHECKON, S. Using Teams-Games-Tournament a t Mark Twain School. ...
Excessive levitation for the efficient loading of large-volume optical dipole traps
Skbn uses an in-memory buffer for the copy process, to avoid excessive memory consumption. Skbn currently supports the following providers: AWS S3 Minio S3 Azure Blob Storage Google Cloud Storage ...
wander程序是一个很好的学习程序,将这个程序弄明白对于先锋三机器人的学习有着很大的帮助,这个wander注释版尽我的能力做了注释,希望对大家有所帮助
GPS详细的资料,我做论文设计自己用过的,希望对大家都有用
A Dielectric-Filled Waveguide Antenna Element for 3D Imaging Radar in High Temperature and Excessive Dust Conditions
过量锂在合成锂离子电池正极材料0.4Li2MnO3•0.6LiNi2/3Mn1/3O2中的作用,李笑娲,顾大伟,本文采用溶胶凝胶法合成了富锂正极材料0.4Li2MnO3•0.6LiNi2/3Mn1/3O2. 通过X射线衍射(XRD),扫描电子显微镜(SEM),傅里叶...