The difference between the sync block synchronized Java method and synchronization

Original link: Add a link description

Most of thread synchronization problem solved using synchronized, there are two ways to synchronize code blocks and synchronization method, remember what the main difference between these two

Test code:

package com.xujingyang.testThread;

public class SynObj{
    public synchronized void showA(){
        System.out.println("showA..");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    public void showB(){
        synchronized (this) {
            System.out.println("showB..");
        }
    }
    
    public void showC(){
        String s="1";
        synchronized (s) {
            System.out.println("showC..");
        }
    }
}
package com.xujingyang.testThread;

public class Test {
    public static void main(String[] args) {
        final SynObj sy=new SynObj();
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                sy.showA();
            }
        }).start();
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                sy.showB();
            }
        }).start();
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                sy.showC();
            }
        }).start();
    }
}

This code is printed result, showA ...... showC ...... will soon be printed out showB ...... time to time will only print out, then why not showB be called as soon as showC it?

After starting the thread 1 calls method A, then thread 1 will sleep for 3 seconds, then calls Method C, Method C noted here with the synchronized lock, where s is the lock object string object. Method B is different, however, is locked with the current object this, note that method A plus synchronized directly on the method, what the object is locked it? Obviously, these two methods used are a lock.

From these results, we know that this is what is locked synchronization method used, because the thread 1 in sleep, when the lock has not released, causes the thread 2 only after three seconds to call the method B, thus, we can see two kind of locking mechanism with a lock object is the same, namely the current object.
  Further, the synchronization method applied directly synchronized method implemented on a lock, the lock synchronization code block within the method, it is clear that the scope of lock synchronization method is relatively large, the range to be synchronized block dots, the larger the range of the general synchronization , the worse the performance, the general need to lock to synchronize the time, certainly the smaller the better, so better performance
.


First, when the same target object in two concurrent threads to access this synchronized (this) synchronized block, a time only one thread to be implemented. Another thread must wait for the current thread to execute the code block after the completion of the implementation of the code block.
2. However, when a thread to access the object of a synchronized (this) synchronized block, another thread can still access the object in a non-synchronized (this) synchronized block.
Third, it is particularly critical when a thread to access the object of a synchronized (this) synchronized block, another object of all other threads synchronized (this) to access the synchronization code block will be blocked.

Guess you like

Origin blog.csdn.net/qq_34749144/article/details/90724610