`
leonzhx
  • 浏览: 797216 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 66: Synchronize access to shared mutable data

阅读更多

1.  Without synchronization, one thread’s changes might not be visible to other threads. Not only does synchronization prevent a thread from observing an object in an inconsistent state, but it ensures that each thread entering a synchronized method or block sees the effects of all previous modifications that were guarded by the same lock.

 

2.  Reading a variable other than a long or double is guaranteed to return a value that was stored into that variable by some thread, even if multiple threads modify the variable concurrently and without synchronization. (atomic)

 

3.  The advice that to improve performance, you should avoid synchronization when reading or writing atomic data is dangerously wrong. Synchronization is required for reliable communication between threads as well as for mutual exclusion.

 

4.  The Thread.stop method, but this method was deprecated long ago because it is inherently unsafe—its use can result in data corruption. Do not use Thread.stop.

 

5.  In the absence of synchronization, it’s quite acceptable for the virtual machine to transform this code:

while (!done)
  i++;

 

into this code:

if (!done)
  while (true)
    i++;

 

This optimization is known as hoisting, and it is precisely what the HotSpot server VM does. 

 

6.  Synchronization has no effect unless both read and write operations are synchronized.

 

7.  While the volatile modifier performs no mutual exclusion, it guarantees that any thread that reads the field will see the most recently written value.

 

8.  Either share immutable data, or don’t share at all. In other words, confine mutable data to a single thread.

 

9.  It is acceptable for one thread to modify a data object for a while and then to share it with other threads, synchronizing only the act of sharing the object reference. Other threads can then read the object without further synchronization, so long as it isn’t modified again. Such objects are said to be effectively immutable. Transferring such an object reference from one thread to others is called safe publication. There are many ways to safely publish an object reference: you can store it in a static field as part of class initialization; you can store it in a volatile field, a final field, or a field that is accessed with normal locking; or you can put it into a concurrent collection.

 

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 78: Synchronize access to shared mutable data Item 79: Avoid excessive synchronization Item 80: Prefer executors, tasks, and streams to threads Item 81: Prefer concurrency utilities to wait and ...

    delphi vcl线程同步synchronize

    Synchronize方法是Delphi中用于在主线程和工作线程之间进行通信的关键工具,确保对用户界面(UI)的操作是安全的。本文将深入探讨Delphi VCL线程同步以及Synchronize方法的工作原理、使用场景和最佳实践。 ### 1. ...

    awesome-readme-to-data-action:一个GitHub Action将出色的自述文件转换为数据并在PR中提交

    name : Awesome readme to data action on : pull_request : types : [opened, synchronize] branches : - master jobs : build : name : awesome readme to data runs-on : ubuntu-latest steps : - uses...

    Big Data and Computational Intelligence in Networking-CRC(2018).pdf

    how to synchronize concurrent parameter updates performed by multiple workers h. Biggraph:Biggraphdecomposition;real-timeandlarge-scalegraph processing; big data security 4. Part 4: Emerging ...

    SQLToolbelt part1

    The SQL Toolbelt allows developers and DBAs to gain access to ALL the SQL Server tools we currently produce, including new releases. This means that when you are working with SQL Server databases, you...

    操作系统概念英文课件:Chapter 6 Process Synchronization.ppt

    One classic solution to the critical-section problem is Peterson's algorithm, which uses two shared variables, 'turn' and 'flag,' to enable two processes to synchronize their entry into the critical ...

    iOS 8 Swift Programming Cookbook Solutions Examples for iOS Apps.pdf

    - **Concept**: CloudKit is a powerful framework that enables developers to store data on Apple's servers and synchronize it across multiple devices. This feature is particularly useful for creating ...

    操作系统英文教学课件:Chapter 4 Threads.ppt

    1. **Synchronization**: Threads need to coordinate access to shared resources to prevent race conditions and ensure data consistency. 2. **Deadlocks**: Threads can become blocked waiting for resources...

    Red-Gate SQL Professional Toolbelt (part1)

    SQL Data Compare™ Pro: compare and synchronize data SQL Packager™: package, upgrade, and install databases SQL Prompt™ Pro: seamless IntelliSense-style code completion and layout SQL Data ...

    Red-Gate SQL Professional Toolbelt (part2)

    SQL Data Compare™ Pro: compare and synchronize data SQL Packager™: package, upgrade, and install databases SQL Prompt™ Pro: seamless IntelliSense-style code completion and layout SQL Data ...

    Red-Gate SQL Professional Toolbelt (part4)

    SQL Data Compare™ Pro: compare and synchronize data SQL Packager™: package, upgrade, and install databases SQL Prompt™ Pro: seamless IntelliSense-style code completion and layout SQL Data ...

    Red-Gate SQL Professional Toolbelt (part3)

    SQL Data Compare™ Pro: compare and synchronize data SQL Packager™: package, upgrade, and install databases SQL Prompt™ Pro: seamless IntelliSense-style code completion and layout SQL Data ...

    Developing Hybrid Applications for the iPhone: Using HTML, CSS, and JavaScript to Build Dynamic Apps for the iPhone

    Using XMLHttpRequest to access or synchronize remote data and use web services Creating Google-based maps for your own app that work like those you see in the iPhone’s Maps application This book ...

    redis令牌机制实现秒杀系统

    1) 悲观锁:synchronize 、 Lock 2) 乐观锁:数据库乐观锁版本号控制 2) 高并发情况下系统压力以及用户体验 解决方案: redis 本教程采用:redis中list类型达到令牌机制完成秒杀。用户抢redis中的令牌,抢到 令牌的...

    FTP_synchronize_0_9_6_1_dll

    FTP_synchronize_0_9_6_1_dll 是一个针对Notepad++文本编辑器的FTP同步插件,主要用于帮助用户实现本地代码与远程服务器之间的文件同步。Notepad++是一款非常流行的开源文本编辑器,尤其受到程序员和Web开发者的喜爱...

    CALL RFC synchronize and asychronize Examples

    以下是关于"CALL RFC synchronize and asychronize Examples"的详细解释。 **同步调用 (Synchronize Call)** 同步调用是立即执行并等待结果返回的方式。在提供的示例`SAPBC415_RFCSYNC_S1`中,我们看到如何通过`...

    Synchronize It 3.5文件夹同步器 简体中文界面

    Synchronize It!还有其他特点如下: 1.可以自订许多的项目,包括过滤器、专案、颜色等等。 2.可以进行多种同步任务独立保存和点用,也可组合多个任务一次同步搞定。 3.支持压缩文件的同步。 4.内建二进位文件的比较...

    java synchronize 线程同步

    Java中的`synchronized`关键字是用于实现线程同步的关键机制,主要目的是解决多线程环境下的数据安全问题。当多个线程访问共享资源时,如果没有适当的同步控制,可能会导致数据不一致或者竞态条件等问题。...

    GDSNProviderUserGuide.pdf

    STEP GDSN Provider User Guide ...GDSN enables trading partners to synchronize data. Any changes to the data pool made by one company are automatically available to all of its trading partners.

    label-sync-action

    标签同步动作name : Synchronize Issue Labelson : workflow_dispatch : push : branches : - masterjobs : milestone : name : Synchronize Issue Labels runs-on : ubuntu-latest steps : - name : Checkout uses ...

Global site tag (gtag.js) - Google Analytics