Introduction to Thread Synchronization basics

Source: Internet
Author: User

A scenario in which multiple threads of execution share a resource is one of the most common concurrent programming scenarios. Often encountered in concurrent applications: multiple threads read or write the same data, or access the same file or database connection. To prevent these shared resources from possible errors or inconsistent data, we must implement mechanisms to prevent these errors from occurring.

In order to solve these problems, the concept of critical area (Critical section) was introduced. A critical section is a block of code that accesses a shared resource, which allows only one thread to execute at the same time.

To help programmers implement this critical section, Java provides a synchronization mechanism. When a thread attempts to access a critical section, it uses a synchronization mechanism to see if another thread has entered the critical section. If no other thread enters the critical section, it can enter the critical section, and if it is already wired into the critical section, it is suspended by the synchronization mechanism until the incoming thread leaves the critical section. If more than one thread waits to enter the critical section, the JVM chooses one and the rest waits.

We'll walk through the two basic synchronization mechanisms provided with Java statements in the following steps:

  • synchronized key mechanisms;

  • Lock interface and its implementation mechanism;

Introduction to Thread Synchronization basics

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.