Java Basics Note (V: multi-threaded synchronization issues)

Source: Internet
Author: User

Multi-threaded programs are often designed to improve the utilization of resources, or to improve the efficiency of the program, or to better monitor the program's running process. The purpose of multithreading synchronization is to allow multiple threads to work in a coordinated manner. Synchronous processing of multithreading can be achieved through synchronous methods and synchronous statement blocks. a Java virtual machine implements these two methods of synchronization by locking the resource, such as memory. Another problem with this mechanism is the deadlock problem (that is, all of the program's threads are in a blocking state or waiting state). Good programming should try to avoid this deadlock problem.

first, the basic principle of multithreading synchronization

If you share resources across multiple concurrent threads, you may need to synchronize. the Java virtual machine implements multi-threaded synchronous processing by locking each object. The objects here include two types of class objects and instance objects. The static and static member methods of a class are subordinate to the class object. A non-static member and non-static member method of a class is an instance object that is not subordinate to a class object and is subordinate to the class.

A Java virtual machine is equipped with a lock and a waiting set for each object. The internal locking of the object is a number of synchronization methods and synchronous statement blocks. One way to be a synchronous method is to add a modifier to the method synchronized . The definition format for a synchronous statement block is as follows:

Synchronized(An expression of reference type)

Statement block

where the keyword synchronized is the guide word for the synchronous statement block; the expression in "()" must be an expression of a reference type, point to a Class object or instance object, which specifies the object associated with the synchronous statement block, and the statement block consists of a pair of " {}"and a series of statements enclosed in braces.

due to the synchronous processing mechanism, A Java virtual machine requires some additional overhead in time and space when running a synchronous method or a synchronous statement block. While using multithreading can improve resource utilization, if you use synchronous methods or synchronous statement blocks too frequently, you can reduce the degree of parallelism of multithreading, and May Ultimately reduce the efficiency of your programs because of the extra overhead of Java virtual machines.

two, multi-threaded synchronization problem (1) deadlock problem

A shortage of resources can cause all threads of the program to fall into a waiting state or a blocking state. The deadlock problem often refers to the problem that is caused by the fact that the resources are not in short supply, but because the programming is unreasonable, all the threads of the program are in a waiting state or blocking state. Typically, each thread holds several resources while it waits for several resources, while the waiting resources are controlled by other threads, so each thread is in a blocking state.

(2) The granularity problem of multithreading synchronization

Java virtual machine through the Java language multithreading features to improve the operation of Java program efficiency. Synchronization is often required between multiple threads due to shared memory and so on. Synchronous processing often reduces the degree of parallelism of threads, which means that some threads cannot be parallel and can only be serialized. Therefore, there is a lot of data that the smaller the granularity of multi-threaded synchronization is better, that is, it is recommended to reduce the amount of code in synchronous and synchronous statement blocks as much as possible, thus shortening the time for multiple threads to run serially. In fact, this will not improve the efficiency of the operation of the program is a question worth discussing. This data ignores the additional overhead required by the Java virtual machine for synchronous processing of threads, that is, if the synchronization method or block of statements is frequently entered and exited, it can also reduce the efficiency of the program's operation. Good multithreading design should do well the balance between the granularity of multi-threaded synchronization and the number of simultaneous processing, so as to really improve the running efficiency of the program.

Java Basics Note (V: multi-threaded synchronization issues)

Related Article

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.