In-depth understanding of multithreading two

Source: Internet
Author: User
Tags volatile

Related to: 1. Use of the 2.synchronized object monitor as class when the object monitor is synchronized 3. How non-threading security occurs. 4. Key role of volatile keywords 5. Keyword volatile and synchronized differences and use Situation   One: Synchronization method:    (1). Non-thread-safe and thread-safe           Non-thread safety occurs when multiple threads are concurrently accessing instance variables in the same object. The result is "read dirty", that is, the data is changed .          and thread safety is to obtain the value of the entity variable is synchronized, there is no dirty read phenomenon .    (2). The variable inside the method is thread safe, that is, the private variable inside the method is not a "non-thread safe" issue .    (3). Instance variables are non-thread-safe, and multiple threads collectively access instance variables (attributes) in an object, and there is a risk of "non-thread-safe" issues .            Two threads must be thread-safe to access the synchronization method in the same object .    (4). Multiple objects multiple locks           First know that the lock acquired by synchronized is an object lock, that is, multiple threads accessing the same object are synchronous (synchronized uses the object monitor is one), but different threads access different objects are not synchronized .          Call keyword Synchronized declaration method must be queued to run, and do not forget why to lock, is because of sharing, only read and write access to the shared resources need to synchronize .          Note:                A thread first holds the lock lock for object, The B thread can invoke the non-synchronized method in the object objects asynchronously                 A thread first holds the Loc of object objectsK-Lock, B-thread If you call the method of the synchronized type in the object at this point, you need to wait, that is, synchronize .    (5). Lock re-entry issues:          When using synchronized, when a thread obtains an object lock, it can again obtain the lock of the object when it is requested again. That is, when a synchronized method/block is called inside the other synchronized method/block of this class, Yes, you can always get a lock. .          reentrant locks are also supported in the inheritance environment of parent-child classes, that is, the synchronized  of the parent class can be called in the Synchronized method of the subclass   (6). Lock release: When an exception occurs, the lock is automatically released     (7). Synchronization does not have inheritance, that is, the subclass inherits the synchronized method of the parent class, but it does not have the Synchronized keyword itself, then the method is not a synchronous method. The synchronization method must precede the synchronized    (8). The synchronous statement block          synchronized method is to lock the current object, The synchronized code block is a lock on an object .          Why do I have a synchronous statement block? is because the synchronized method has the disadvantage, is the running time relatively long, how to solve? is to use the synchronous code block, but note that this synchronization code block is to lock the read and write of the shared resource, do not lock the contents of the whole method, when a thread accesses a synchronized synchronous code block of object, Another thread can access the non-synchronized (this) block of code in the object objects .          that is, half synchronous, half asynchronous, not in the synchronized block is asynchronous execution, In the synchronized block is the synchronous execution of .    (9). Use arbitrary objects as object monitors .          using synchronized (this) The current object is locked. Using synchronous methods and synchronizing blocks of code has two functions: &nbsP           1). Synchronized synchronous method or synchronized (this) synchronous code block call blocking state         & nbsp   2). At the same time, only one thread can execute the code in the synchronized synchronization method or synchronized (this) synchronization code block           Using synchronized (not this object x) to synchronize blocks of code, the advantage: if there are many synchroniized methods in a class, the synchronization can be implemented, but it will be blocked, affecting the efficiency of operation, but if the use of synchronous code block lock is not the This object, The program in the synchronized (non-this) code block is asynchronous and does not compete with the other lock synchronization methods for this lock, which can greatly improve the efficiency of the operation.           use synchronized (not this object x) to synchronize the code block format, the object monitor must be The same object, cannot be an object within a method (variables, etc.).
        Multiple threads calling the same method is random, what does it mean? This means that one thread executes this method synchronously, but multiple threads call this method multiple times at the same time, and two threads are asynchronous .    (10). Three conclusions:            1). Render sync effects when multiple threads execute synchronized (x) {} synchronously code block at the same time         & nbsp   2). When other threads execute the synchronized synchronization method in the X object, the synchronization effect is rendered             3). When other threads execute the X object method inside the synchronized ( This) also renders a sync effect on the block of code     (11). Static synchronous synchronized method with synchronized (class) code block .            The Synchronized keyword to static method is to lock class class, add to non-static method is to lock the object. Class locks can work on all object instances of a class., which means that different objects are also executed synchronously .    (12). Constant pool properties for data type string:            A function in the JVM that has a string constant pool cache. That is, string a = "a"; String B = "a"; System.out.println (a==b); The result is true, which causes the string type to be locked, if the string value is the same, then two threads have the same lock, so in most cases The synchronous synchronized code block does not use string as the lock object, but instead uses the other, such as new object () to instantiate a Objectt object, but it is not placed in the cache.                 (13). Synchronous synchronized Method Infinite wait and resolve:        &NB Sp  Synchronous Synchronized method If there is a dead loop inside, it causes the thread to wait indefinitely, the workaround is to change the synchronized method to a non-this synchronous code block, so that the synchronization lock is not the same object, so that it can be executed asynchronously .    (14 ). Multi-threaded deadlock:            Different threads are waiting for a lock that cannot be released, causing all tasks to fail to finish, and simply to wait for each other. As long as we wait for each other to release the lock, there's a possibility of a deadlock. .            When designing a program, avoid holding each other's locks on each other    . Volatile keyword: Make variables visible across multiple threads .            is forcing the value of a variable from the public stack instead of getting the value of the variable from the thread private data stack .    (16). Comparison of synchronized and volatile:            1). Volatile is a lightweight implementation of thread synchronization and can only be modified with variables, and synchronized can modify the method as well as the code block             2). Multithreaded access volatile does not block, and synchronized is blocked .            3). Volatile can guarantee the visibility of the data, but it cannot guarantee atomicity, synchronized can guarantee atomicity, and can indirectly guarantee visibility, Because it synchronizes the private memory with the data in public memory .            4). The keyword volatile solves the variable's visibility between multiple threads; The Synchronized keyword solves the synchronization of accessing resources between multiple threads.

In-depth understanding of multithreading two

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.