线程安全性(一) 原子性 基于锁的实现 - synchronized修饰方法

synchronized修饰方法 - 正例

  • 同一个对象不同线程中访问同步方法,同步方法对这些线程是起到同步作用的;
  • 如果SynchronizedMethod是个父类,那么子类中的test2()是带不上synchronized的;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedMethod {

    public synchronized void test2(int j) {
        for (int i = 0; i < 10; i++) {
            log.info("synchronized method object {} - {}", j, i);
        }
    }

    public static void main(String[] args) {
        testMethodSync();
    }

    private static void testMethodSync() {
        SynchronizedMethod example1 = new SynchronizedMethod();
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(() -> {
            example1.test2(1);
        });
        service.execute(() -> {
            example1.test2(1);
        });
    }

}

输出:

  • 同一个对象所在的2个线程按顺序先后执行;

17:14:54.487 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 0
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 1
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 2
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 3
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 4
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 5
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 6
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 7
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 8
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 9
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 0
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 1
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 2
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 3
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 4
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 5
17:14:54.493 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 6
17:14:54.493 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 7
17:14:54.493 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 8
17:14:54.493 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 9

synchronized修饰方法 - 反例

  • 不同对象不同线程中访问同步方法,同步方法对不同调用对象是不能起到同步作用的;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedMethod {

    public synchronized void test2(int j) {
        for (int i = 0; i < 10; i++) {
            log.info("synchronized method object {} - {}", j, i);
        }
    }

    public static void main(String[] args) {
        testMethodNotSync();
    }

  private static void testMethodNotSync() {
        SynchronizedMethod example1 = new SynchronizedMethod();
        SynchronizedMethod example2 = new SynchronizedMethod();
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(() -> {
            example1.test2(1);
        });
        service.execute(() -> {
            example2.test2(2);
        });
    }

}

输出:

  • 乱序的,两个对象所在的线程同时执行;

17:19:00.185 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 0
17:19:00.185 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 0
17:19:00.190 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 1
17:19:00.190 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 1
17:19:00.190 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 2
17:19:00.190 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 2
17:19:00.190 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 3
17:19:00.190 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 3
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 4
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 4
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 5
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 5
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 6
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 6
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 7
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 7
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 8
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 8
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 9
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值