Java BASICS (5: multi-thread synchronization)

Source: Internet
Author: User

Java BASICS (5: multi-thread synchronization)

Multi-threaded programs are often written to improve resource utilization, program running efficiency, or better monitor the program running process. Multi-threaded synchronous processing is designed to allow multiple threads to work in parallel. Synchronous processing of multiple threads can be achieved through Synchronization Methods and synchronization statement blocks. Java virtual machine locks resources (such as memory) to implement these two Synchronization Methods. Another problem caused by this mechanism is the deadlock (that is, all threads of the program are in the blocking or waiting state ). Good program design should avoid this deadlock problem.

I. Basic principles of multi-thread synchronization

If resources are shared among multiple concurrent threads, synchronous processing may be required. The Java virtual machine locks each object to synchronize multiple threads. The objects here include class objects and instance objects. The static member fields and static member methods of a class belong to class objects. The non-static member fields and non-static member methods of a class do not belong to class objects, but belong to class instance objects.

The Java virtual machine provides a lock and a waiting set for each object. The Object locks some Synchronization Methods and synchronization statement blocks. To make a method a synchronous method, you only need to add the modifier synchronized to the method. The Definition Format of the synchronous statement block is as follows:

Synchronized (Expression of reference type)

Statement Block

The key word synchronized is the guiding word of the synchronous statement block. The expression in "()" must be a reference expression that points to a class object or instance object, that is, the object associated with the synchronization statement block is specified. The statement block consists of a pair of "{}" and a series of statements enclosed in braces.

Due to the synchronization processing mechanism, Java virtual machine requires some additional overhead in time and space when running Synchronization Methods or synchronous statement blocks. Although multithreading can improve resource utilization, if the synchronous method or statement block is used too frequently, the parallel degree of Multithreading is reduced, the extra overhead of the Java Virtual Machine may cause the program to run more efficiently.

Ii. multi-thread synchronization (1) deadlock

Resource shortage may cause all threads of the program to fall into the waiting or blocking state. Deadlock often refers to a problem that causes this situation: resources are not in short supply, but all threads of the program are in the waiting or blocking state due to unreasonable program design. A typical scenario is that each thread occupies several resources while waiting for several resources, and the waiting resources are controlled by other threads, so every thread is in a blocking state.

(2) granularity of multi-thread synchronization

Java Virtual Machine improves the running efficiency of Java programs through the multi-thread feature of Java. Synchronization is often required between multiple threads because of shared memory. Synchronous processing often reduces the degree of parallelism of threads, that is, some threads cannot be parallel but can only be serialized. Therefore, many documents believe that the smaller the granularity of multi-thread synchronization, the better. That is, we recommend that you reduce the amount of code in the synchronous method and synchronous statement block as much as possible to shorten the serial running time of multiple threads. In fact, it is worth discussing whether it will improve the program running efficiency. These materials ignore the additional overhead required by Java Virtual Machine for thread synchronization, that is, if you frequently access and exit the synchronization method or synchronous statement block, it will also reduce the program running efficiency. A good multi-threaded program design should balance the granularity of multi-threaded synchronization with the number of times of synchronization processing, so as to truly improve the program running efficiency.

 

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.