Java's gorgeous turning and multithreading (2) -- Synchronization

Source: Internet
Author: User

When multiple threads run at the same time, the execution sequence cannot be determined. If these threads operate on a public resource at the same time, the final execution result is unpredictable, therefore, some methods should be taken in the code to protect public resources and ensure that only one thread is allowed to access these resources at the same time.

 


Thread Synchronization Mechanism:

1. multi-thread synchronization relies on the object lock mechanism. Behind the synchronized keyword is the use of blocking to achieve mutex access to shared resources.


2. The mutex lock of objects ensures the integrity of shared data operations. Each object corresponds to a tag that can be called a mutex lock. This tag ensures that only one thread can access the object at any time.

 

3. Thread Synchronization in Java is completed by obtaining the object monitor. Each object instance has a monitor. We can regard this monitor as a lock. Before the thread executes the synchronization operation, it will first perform the lock operation on the object instance, that is, the synchronized Method for accessing an object, no other threads can access the synchronized method.

 

4. After the synchronization code is executed, the monitor will be released. If other threads want to perform the same operation, the wait will be blocked when obtaining the object monitor, until the previous thread releases the monitor. JVM ensures that only one thread can obtain an object monitor at a time.


 

Example:

public class Test implements Runnable{     public synchronized void run(){     for(int i = 0;i < 10; i++){     System.out.print(" " + i); } } public static void main(String[] args){     Test t = new Test(); Thread t1 = new Thread(t); Thread t2 = new Thread(t); t1.start(); t2.start(); }}

If no lock is applied, the implementation result is as follows:


Locks are implemented as follows: and the running result is 0-9 no matter how many times it is run.



Synchronization can be implemented in the following ways:


Wait (): puts a thread in the waiting state and releases the lock of the object it holds.

Sleep (): To make a running thread Sleep and make a static method, call this method to capture InterruptedException exceptions.

Join (): used to wait for the end of another thread.

Running y (): Wake up a thread in the waiting state. When calling this method, the JVM cannot exactly wake up a thread in the waiting state, but determines which thread to wake up, rather than by priority.

Ally y (): Wake up all threads in the waiting state. Note that it is not a lock for all wake-up threads but a lock for them to compete.


Summary:

It should be noted that no matter whether the synchronized keyword is clipped to the method or object, the lock he acquires is an object, not a piece of code or function. Synchronization is a highly open operation. You should minimize the amount of content to be synchronized.

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.