并发编程(一) synchronized用法

背景:JDK13、IDEA2019.2.4、Gradle6.0.1

一、synchronized修饰整个类

package com.zsx.demo;

import lombok.extern.log4j.Log4j2;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@Log4j2
public class SyncClass {

    public void print() {
        synchronized (SyncClass.class) {
            try {
                log.info("Current thread {} enters the print method, enter current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
                TimeUnit.SECONDS.sleep(2);
                log.info("Current thread {} exits the print method, exit current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                SyncClass syncClass = new SyncClass();
                syncClass.print();
            }, String.format("t%d", i)).start();
        }
    }

}

1.运行结果

11:03:19.181 [t0] INFO com.zsx.demo.SyncClass - Current thread t0 enters the print method, enter current time : 2019-12-05T11:03:19.177354600
11:03:21.186 [t0] INFO com.zsx.demo.SyncClass - Current thread t0 exits the print method, exit current time : 2019-12-05T11:03:21.186224100
11:03:21.186 [t9] INFO com.zsx.demo.SyncClass - Current thread t9 enters the print method, enter current time : 2019-12-05T11:03:21.186224100
11:03:23.187 [t9] INFO com.zsx.demo.SyncClass - Current thread t9 exits the print method, exit current time : 2019-12-05T11:03:23.187631400
11:03:23.187 [t8] INFO com.zsx.demo.SyncClass - Current thread t8 enters the print method, enter current time : 2019-12-05T11:03:23.187631400
11:03:25.188 [t8] INFO com.zsx.demo.SyncClass - Current thread t8 exits the print method, exit current time : 2019-12-05T11:03:25.188517
11:03:25.189 [t7] INFO com.zsx.demo.SyncClass - Current thread t7 enters the print method, enter current time : 2019-12-05T11:03:25.189326600
11:03:27.191 [t7] INFO com.zsx.demo.SyncClass - Current thread t7 exits the print method, exit current time : 2019-12-05T11:03:27.191157100
11:03:27.191 [t6] INFO com.zsx.demo.SyncClass - Current thread t6 enters the print method, enter current time : 2019-12-05T11:03:27.191157100
11:03:29.192 [t6] INFO com.zsx.demo.SyncClass - Current thread t6 exits the print method, exit current time : 2019-12-05T11:03:29.192575300
11:03:29.193 [t5] INFO com.zsx.demo.SyncClass - Current thread t5 enters the print method, enter current time : 2019-12-05T11:03:29.192575300
11:03:31.194 [t5] INFO com.zsx.demo.SyncClass - Current thread t5 exits the print method, exit current time : 2019-12-05T11:03:31.194449800
11:03:31.195 [t4] INFO com.zsx.demo.SyncClass - Current thread t4 enters the print method, enter current time : 2019-12-05T11:03:31.195223600
11:03:33.196 [t4] INFO com.zsx.demo.SyncClass - Current thread t4 exits the print method, exit current time : 2019-12-05T11:03:33.196099900
11:03:33.196 [t2] INFO com.zsx.demo.SyncClass - Current thread t2 enters the print method, enter current time : 2019-12-05T11:03:33.196099900
11:03:35.196 [t2] INFO com.zsx.demo.SyncClass - Current thread t2 exits the print method, exit current time : 2019-12-05T11:03:35.196564400
11:03:35.197 [t3] INFO com.zsx.demo.SyncClass - Current thread t3 enters the print method, enter current time : 2019-12-05T11:03:35.196564400
11:03:37.198 [t3] INFO com.zsx.demo.SyncClass - Current thread t3 exits the print method, exit current time : 2019-12-05T11:03:37.198396300
11:03:37.199 [t1] INFO com.zsx.demo.SyncClass - Current thread t1 enters the print method, enter current time : 2019-12-05T11:03:37.199200600
11:03:39.200 [t1] INFO com.zsx.demo.SyncClass - Current thread t1 exits the print method, exit current time : 2019-12-05T11:03:39.200053200

二、修饰整个代码块

package com.zsx.demo;

import lombok.extern.log4j.Log4j2;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@Log4j2
public class SyncObject {

    private final Object obj = new Object();

    public void print() {
        synchronized (this) {
            try {
                log.info("Current thread {} enters the print method, enter current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
                TimeUnit.SECONDS.sleep(2);
                log.info("Current thread {} exits the print method, exit current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void print1() {
        synchronized (obj) {
            try {
                log.info("Current thread {} enters the print method, enter current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
                TimeUnit.SECONDS.sleep(2);
                log.info("Current thread {} exits the print method, exit current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                SyncObject syncClass = new SyncObject();
                syncClass.print();
            }, String.format("t%d", i)).start();
        }
        for (int i = 5; i < 10; i++) {
            new Thread(()->{
                SyncObject syncObject = new SyncObject();
                syncObject.print1();
            }, String.format("t%d", i)).start();
        }
    }
}

2.运行结果

11:06:08.369 [t4] INFO com.zsx.demo.SyncObject - Current thread t4 enters the print method, enter current time : 2019-12-05T11:06:08.363915
11:06:08.369 [t2] INFO com.zsx.demo.SyncObject - Current thread t2 enters the print method, enter current time : 2019-12-05T11:06:08.363915
11:06:08.369 [t3] INFO com.zsx.demo.SyncObject - Current thread t3 enters the print method, enter current time : 2019-12-05T11:06:08.364915700
11:06:08.369 [t6] INFO com.zsx.demo.SyncObject - Current thread t6 enters the print method, enter current time : 2019-12-05T11:06:08.363915
11:06:08.369 [t8] INFO com.zsx.demo.SyncObject - Current thread t8 enters the print method, enter current time : 2019-12-05T11:06:08.363915
11:06:08.369 [t9] INFO com.zsx.demo.SyncObject - Current thread t9 enters the print method, enter current time : 2019-12-05T11:06:08.364915700
11:06:08.369 [t1] INFO com.zsx.demo.SyncObject - Current thread t1 enters the print method, enter current time : 2019-12-05T11:06:08.363915
11:06:08.369 [t7] INFO com.zsx.demo.SyncObject - Current thread t7 enters the print method, enter current time : 2019-12-05T11:06:08.363915
11:06:08.369 [t5] INFO com.zsx.demo.SyncObject - Current thread t5 enters the print method, enter current time : 2019-12-05T11:06:08.363915
11:06:08.369 [t0] INFO com.zsx.demo.SyncObject - Current thread t0 enters the print method, enter current time : 2019-12-05T11:06:08.363915
11:06:10.373 [t4] INFO com.zsx.demo.SyncObject - Current thread t4 exits the print method, exit current time : 2019-12-05T11:06:10.373778100
11:06:10.373 [t2] INFO com.zsx.demo.SyncObject - Current thread t2 exits the print method, exit current time : 2019-12-05T11:06:10.373778100
11:06:10.373 [t9] INFO com.zsx.demo.SyncObject - Current thread t9 exits the print method, exit current time : 2019-12-05T11:06:10.373778100
11:06:10.373 [t1] INFO com.zsx.demo.SyncObject - Current thread t1 exits the print method, exit current time : 2019-12-05T11:06:10.373778100
11:06:10.373 [t0] INFO com.zsx.demo.SyncObject - Current thread t0 exits the print method, exit current time : 2019-12-05T11:06:10.373778100
11:06:10.373 [t8] INFO com.zsx.demo.SyncObject - Current thread t8 exits the print method, exit current time : 2019-12-05T11:06:10.373778100
11:06:10.373 [t3] INFO com.zsx.demo.SyncObject - Current thread t3 exits the print method, exit current time : 2019-12-05T11:06:10.373778100
11:06:10.373 [t6] INFO com.zsx.demo.SyncObject - Current thread t6 exits the print method, exit current time : 2019-12-05T11:06:10.373778100
11:06:10.373 [t7] INFO com.zsx.demo.SyncObject - Current thread t7 exits the print method, exit current time : 2019-12-05T11:06:10.373778100
11:06:10.373 [t5] INFO com.zsx.demo.SyncObject - Current thread t5 exits the print method, exit current time : 2019-12-05T11:06:10.373778100

 三、修饰普通方法

package com.zsx.demo;

import lombok.extern.log4j.Log4j2;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@Log4j2
public class SyncMethod {

    public synchronized void print() {
        try {
            log.info("Current thread {} enters the print method, enter current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
            TimeUnit.SECONDS.sleep(2);
            log.info("Current thread {} exits the print method, exit current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                SyncMethod syncMethod = new SyncMethod();
                syncMethod.print();
            }, String.format("t%d", i)).start();
        }
    }
}

3.运行结果

11:12:18.807 [t7] INFO com.zsx.demo.SyncMethod - Current thread t7 enters the print method, enter current time : 2019-12-05T11:12:18.799293
11:12:18.806 [t5] INFO com.zsx.demo.SyncMethod - Current thread t5 enters the print method, enter current time : 2019-12-05T11:12:18.799293
11:12:18.806 [t8] INFO com.zsx.demo.SyncMethod - Current thread t8 enters the print method, enter current time : 2019-12-05T11:12:18.799293
11:12:18.806 [t1] INFO com.zsx.demo.SyncMethod - Current thread t1 enters the print method, enter current time : 2019-12-05T11:12:18.799293
11:12:18.806 [t2] INFO com.zsx.demo.SyncMethod - Current thread t2 enters the print method, enter current time : 2019-12-05T11:12:18.799293
11:12:18.806 [t6] INFO com.zsx.demo.SyncMethod - Current thread t6 enters the print method, enter current time : 2019-12-05T11:12:18.799293
11:12:18.806 [t3] INFO com.zsx.demo.SyncMethod - Current thread t3 enters the print method, enter current time : 2019-12-05T11:12:18.799293
11:12:18.806 [t9] INFO com.zsx.demo.SyncMethod - Current thread t9 enters the print method, enter current time : 2019-12-05T11:12:18.799293
11:12:18.806 [t0] INFO com.zsx.demo.SyncMethod - Current thread t0 enters the print method, enter current time : 2019-12-05T11:12:18.799293
11:12:18.806 [t4] INFO com.zsx.demo.SyncMethod - Current thread t4 enters the print method, enter current time : 2019-12-05T11:12:18.799293
11:12:20.811 [t7] INFO com.zsx.demo.SyncMethod - Current thread t7 exits the print method, exit current time : 2019-12-05T11:12:20.811938900
11:12:20.811 [t8] INFO com.zsx.demo.SyncMethod - Current thread t8 exits the print method, exit current time : 2019-12-05T11:12:20.811938900
11:12:20.811 [t2] INFO com.zsx.demo.SyncMethod - Current thread t2 exits the print method, exit current time : 2019-12-05T11:12:20.811938900
11:12:20.811 [t1] INFO com.zsx.demo.SyncMethod - Current thread t1 exits the print method, exit current time : 2019-12-05T11:12:20.811938900
11:12:20.811 [t5] INFO com.zsx.demo.SyncMethod - Current thread t5 exits the print method, exit current time : 2019-12-05T11:12:20.811938900
11:12:20.811 [t6] INFO com.zsx.demo.SyncMethod - Current thread t6 exits the print method, exit current time : 2019-12-05T11:12:20.811938900
11:12:20.813 [t0] INFO com.zsx.demo.SyncMethod - Current thread t0 exits the print method, exit current time : 2019-12-05T11:12:20.812912200
11:12:20.811 [t4] INFO com.zsx.demo.SyncMethod - Current thread t4 exits the print method, exit current time : 2019-12-05T11:12:20.811938900
11:12:20.811 [t3] INFO com.zsx.demo.SyncMethod - Current thread t3 exits the print method, exit current time : 2019-12-05T11:12:20.811938900
11:12:20.811 [t9] INFO com.zsx.demo.SyncMethod - Current thread t9 exits the print method, exit current time : 2019-12-05T11:12:20.811938900

四、修饰静态方法

package com.zsx.demo;

import lombok.extern.log4j.Log4j2;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@Log4j2
public class SyncStaticMethod {

    public synchronized static void print() {
        try {
            log.info("Current thread {} enters the print method, enter current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
            TimeUnit.SECONDS.sleep(2);
            log.info("Current thread {} exits the print method, exit current time : {}", Thread.currentThread().getName(), LocalDateTime.now());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(SyncStaticMethod :: print, String.format("t%d", i)).start();
        }
    }
}

4.运行结果

11:15:25.208 [t0] INFO com.zsx.demo.SyncStaticMethod - Current thread t0 enters the print method, enter current time : 2019-12-05T11:15:25.203808800
11:15:27.213 [t0] INFO com.zsx.demo.SyncStaticMethod - Current thread t0 exits the print method, exit current time : 2019-12-05T11:15:27.213676200
11:15:27.214 [t9] INFO com.zsx.demo.SyncStaticMethod - Current thread t9 enters the print method, enter current time : 2019-12-05T11:15:27.214485300
11:15:29.216 [t9] INFO com.zsx.demo.SyncStaticMethod - Current thread t9 exits the print method, exit current time : 2019-12-05T11:15:29.216422600
11:15:29.217 [t8] INFO com.zsx.demo.SyncStaticMethod - Current thread t8 enters the print method, enter current time : 2019-12-05T11:15:29.217092600
11:15:31.217 [t8] INFO com.zsx.demo.SyncStaticMethod - Current thread t8 exits the print method, exit current time : 2019-12-05T11:15:31.217971200
11:15:31.218 [t7] INFO com.zsx.demo.SyncStaticMethod - Current thread t7 enters the print method, enter current time : 2019-12-05T11:15:31.218730300
11:15:33.219 [t7] INFO com.zsx.demo.SyncStaticMethod - Current thread t7 exits the print method, exit current time : 2019-12-05T11:15:33.219578900
11:15:33.220 [t6] INFO com.zsx.demo.SyncStaticMethod - Current thread t6 enters the print method, enter current time : 2019-12-05T11:15:33.220374700
11:15:35.221 [t6] INFO com.zsx.demo.SyncStaticMethod - Current thread t6 exits the print method, exit current time : 2019-12-05T11:15:35.221247200
11:15:35.222 [t5] INFO com.zsx.demo.SyncStaticMethod - Current thread t5 enters the print method, enter current time : 2019-12-05T11:15:35.222022
11:15:37.222 [t5] INFO com.zsx.demo.SyncStaticMethod - Current thread t5 exits the print method, exit current time : 2019-12-05T11:15:37.222912900
11:15:37.223 [t4] INFO com.zsx.demo.SyncStaticMethod - Current thread t4 enters the print method, enter current time : 2019-12-05T11:15:37.223708700
11:15:39.224 [t4] INFO com.zsx.demo.SyncStaticMethod - Current thread t4 exits the print method, exit current time : 2019-12-05T11:15:39.224320900
11:15:39.225 [t3] INFO com.zsx.demo.SyncStaticMethod - Current thread t3 enters the print method, enter current time : 2019-12-05T11:15:39.224320900
11:15:41.226 [t3] INFO com.zsx.demo.SyncStaticMethod - Current thread t3 exits the print method, exit current time : 2019-12-05T11:15:41.226252
11:15:41.226 [t2] INFO com.zsx.demo.SyncStaticMethod - Current thread t2 enters the print method, enter current time : 2019-12-05T11:15:41.226252
11:15:43.226 [t2] INFO com.zsx.demo.SyncStaticMethod - Current thread t2 exits the print method, exit current time : 2019-12-05T11:15:43.226619
11:15:43.227 [t1] INFO com.zsx.demo.SyncStaticMethod - Current thread t1 enters the print method, enter current time : 2019-12-05T11:15:43.226619
11:15:45.228 [t1] INFO com.zsx.demo.SyncStaticMethod - Current thread t1 exits the print method, exit current time : 2019-12-05T11:15:45.228400500
发布了129 篇原创文章 · 获赞 14 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/zsx18273117003/article/details/103400614