Java Concurrency programming Combat (Java Concurrency in practice)

Source: Internet
Author: User

Chapter I.Threads share process-wide resources, but each thread has its own program counters, stacks, and local variables. Multiple threads can be scheduled to run on multiple CPUs at the same time. The benefits of threading? In a service application, you can increase resource utilization and system throughput, and take advantage of the power of multiprocessor capabilities. Thread's priority execution time thread switching requires additional overheadChapter IIIf multiple threads access the same mutable state variable without using the appropriate synchronization, then the program will have an error and there are three ways to fix the problem. 1. Do not share the state variable 2 between threads, change the state variable to immutable variable 3, use synchronization when accessing the state variable what is thread safety? Own Baidu stateless objects must be thread-safe attention to the problem of count++ such operations: not atomic, actually three operations read-modify-write what is a race condition? In concurrent programming, the results become unreliable due to improper execution of timing. The common race condition type is "check first and then execute", such as common lazy loading of objectsA package: Java.concurrent.atomic built-in lock: lock mechanism, built-in mechanism for ensuring atomicity in Java. Synchronous code block, synchronous method to synchronized to decorate the method called synchronous method, the lock of the synchronous code block is the object of the method call, staticthe Synchronized method takes a class object as a lockEach Java object can be used as a lock that implements a synchronization, called a built-in lock or a monitor lock. The thread automatically obtains the lock before it enters the synchronization code block and automatically releases the lock when it exits the synchronization code block. The built-in lock is reentrant. Understand the sentence: state variables are protected by this lock. Lock do not misuse, synchronous code block too general cause bad concurrency, greatly slow down program performance. Do not hold locks when performing long computations or operations that may not be completed quickly (such as network I/O, console I/Os).Chapter IIIThe synchronization mechanism is not just to achieve atomicity or to determine a "critical section", but also to ensure the visibility of memory operations between multiple threads. The implication of locking is not limited to mutex behavior, but also includes memory visibility. Note reordering is minimal security: When a thread reads a variable without synchronizing, it may have an aging value, but at least the value is set by a previous thread, not a random value. A 64-bit numeric variable of non-volatile type (double and long) does not apply to minimum security because the JVM allows 64-bit read operations or writes to be decomposed into two 32-bit operations a slightly weaker synchronization mechanism, the volatile variable. The locking mechanism ensures visibility and ensures atomicity, while volatile variables only ensure visibility. (Ensure that only a single thread can update the value of a variablevolatile variable)Publishing an object means that an object can be used in code outside the current scope. Escape is called escaping when an object that should not be published is published. How to publish an object: The simplest way to publish an object is to save the object's reference to a public static variable. Returns a reference to an object from a non-private method, such as a common get Set method in Java Web development. Publishes an instance of an inner class (The inner class instance contains a reference to an instance of an external class) do not use this reference to escape during construction (only if the this reference should escape from the thread when the constructor returns) the thread is closed if the data is accessed on a single range, no synchronization is required. This technique is called thread closure. Implement thread closure method: Ad-hoc thread closed (understanding) stack closed (local variable inside method Access object, familiar with) Threadlocal class (Common)Threadlocal classThis class enables a value in a thread to be associated with the object that holds the value. The class provides access interfaces or methods such as get and set, which have a separate copy of each thread that uses the variable, so get always returns the latest value set by the current thread when it calls set. When a thread terminates, these values are garbage collected. Immutable objects must be thread-safe when the object is immutable: The object is created after its state is not modifiable all fields of the object are final type objects are created correctly (when the object is created, the this reference does not escape)

Java Concurrency programming Combat (Java Concurrency in practice)

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.