Effective JAVA Chapter Tenth concurrency

Source: Internet
Author: User

Effective JAVA Chapter Tenth concurrency

66. Synchronizing access to shared mutable data

  The *java language specification guarantees that reading or writing a variable is atomic (the value returned is guaranteed to be stored in the variable by a thread), unless the variable is of type long or double. (There is no guarantee that the value written by one thread is visible to another thread)

*synchronized retouching method, synchronized code block can be implemented synchronously

*volatile modified variables are only guaranteed to read the most recent value in main memory instead of the copy of the value in RAM, using a volatile variable must be followed (that is, the variable is really independent of the other variables and its own previous values), at this time in consideration of performance improvement in place of the Synchronized keyword

67. Avoid over-synchronization

  * Excessive synchronization can result in degraded performance, deadlock, and even indeterminate behavior.

* Do not call the external indeterminate method in the synchronization method or the synchronous code fast, may cause the exception, deadlock or data corruption.

* You should do as little work as possible within the sync area.

68.executor and tasks take precedence over threads

  Many highly concurrent tool classes are available in the *java.util.concurrent package

  

69. Concurrency tools take precedence over wait and notify

  * If you must use wait and notify code, be sure to always use standard mode to call wait from within the while loop. Priority should be given to using Notifyall instead of notify.

70. Documentation of thread Safety

71. Use lazy initialization with caution

  * Lazy initialization is a double-edged sword, unless necessary (initializing the domain is expensive), otherwise do not

* Multi-threaded Access deferred initialization objects are handled in two ways

 Public Static synchronizedgetinstance () {if(instance==NULL){              return NewInstance (); }          returninstance;} Public Staticgetinstance () {if(instance==NULL){               synchronized( This){                      if(instance==NULL){                            return NewInstance (); }                }            }          returninstance;}

72. Do not rely on the thread scheduler Scheduler

  * Thread Scheduler relies on the policies adopted by the operating system, which is not conducive to the porting of programs

* The best way to write robust, good, and portable multithreaded programs is to ensure that the average number of running threads is not significantly greater than the number of processors

* Let each executing thread do meaningful work, not always waiting

* Thread priority is the most non-portable feature on the Java Platform (Thread.Sleep (), Thread.yield () is typically used for testing)

73. Avoid using Thread group Threadgroup)

* Thread groups do not provide much useful functionality, and many of the features they provide are flawed. You can ignore it, and if you need to handle logical groups of threads, you should probably use the thread pool executor.

Effective JAVA Chapter Tenth concurrency

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.