Concurrency and high concurrency (eight) - thread safety - atomicity -synchronized

Foreword

Leisure time, to talk about the synchronization lock synchronized been exposed in a multithreaded tutorial, equivalent to review it again.

main introduction

synchronized: dependent on JVM

Lock: dependent special CPU instructions, code implementation, ReetrantLock

Main content

First, we first explain about the main synchronization lock synchronized in scope.

1. The code block modification: scope - braces code, applied to calling the object code block , if the different objects will not call the block synchronization.

2. modification methods: scope - the entire method, the role of the object to calling this method .

3. Modified static method: scope - entire static method, the role of all the objects in this class .

4. Dress Class: -synchronized scope later bracketed portion, acting on all the objects of this class (PS: two threads call this synchronization statement on different objects of the same class will also be synchronized ) .

Second, the next we were for synchronized modification of these four cases write four examples.

1. First, write a method that allows synchronized modification of the code block .

    /**
     * Modified the code block
     */
    public void test1(){
        synchronized (this) {
            for(int i=0;i<10;i++){
                log.info("test1-{}",i);
            }
        }
    }
    
    public static void main(String[] args){
        SyncDecorate sd = new SyncDecorate();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->{
            sd.test1 ();
        });
        executorService.execute(()->{
            sd.test1 ();
        });
    }        

Explanation: Here we use the thread pool creates two threads are synchronized block access test1 methods, ranging from a second thread in fact the first thread is finished, began to visit test1 method, but the method in code block test1 Since the visit of the first thread was locked, so the second thread has to wait for the first thread has finished the implementation of this method. Thus execution result is as follows:

23:42:39.348 [pool-1-thread-1] INFO com.controller.synchronize.SyncDecorate - test1-0
23:42:39.351 [pool-1-thread-1] INFO com.controller.synchronize.SyncDecorate - test1-1
23:42:39.351 [pool-1-thread-1] INFO com.controller.synchronize.SyncDecorate - test1-2
23:42:39.351 [pool-1-thread-1] INFO com.controller.synchronize.SyncDecorate - test1-3
23:42:39.351 [pool-1-thread-1] INFO com.controller.synchronize.SyncDecorate - test1-4
23:42:39.351 [pool-1-thread-1] INFO com.controller.synchronize.SyncDecorate - test1-5
23:42:39.351 [pool-1-thread-1] INFO com.controller.synchronize.SyncDecorate - test1-6
23:42:39.351 [pool-1-thread-1] INFO com.controller.synchronize.SyncDecorate - test1-7
23:42:39.351 [pool-1-thread-1] INFO com.controller.synchronize.SyncDecorate - test1-8
23:42:39.351 [pool-1-thread-1] INFO com.controller.synchronize.SyncDecorate - test1-9
23:42:39.351 [pool-1-thread-2] INFO com.controller.synchronize.SyncDecorate - test1-0
23:42:39.351 [pool-1-thread-2] INFO com.controller.synchronize.SyncDecorate - test1-1
23:42:39.351 [pool-1-thread-2] INFO com.controller.synchronize.SyncDecorate - test1-2
23:42:39.351 [pool-1-thread-2] INFO com.controller.synchronize.SyncDecorate - test1-3
23:42:39.351 [pool-1-thread-2] INFO com.controller.synchronize.SyncDecorate - test1-4
23:42:39.351 [pool-1-thread-2] INFO com.controller.synchronize.SyncDecorate - test1-5
23:42:39.351 [pool-1-thread-2] INFO com.controller.synchronize.SyncDecorate - test1-6
23:42:39.351 [pool-1-thread-2] INFO com.controller.synchronize.SyncDecorate - test1-7
23:42:39.351 [pool-1-thread-2] INFO com.controller.synchronize.SyncDecorate - test1-8
23:42:39.351 [pool-1-thread-2] INFO com.controller.synchronize.SyncDecorate - test1-9

Guess you like

Origin www.cnblogs.com/jmy520/p/12041592.html