스레드의 동기화 된 객체 잠금, 더티 읽기, 잠금 재진입


동기화 된 개체 잠금

package com.hanyxx.thread;

import lombok.SneakyThrows;

/**
 * synchronized关键字会锁住整个对象中被synchronized修饰的方法。
 * 运行结果:methodOne方法执行,2秒后,methodTwo方法执行
 * @author layman
 * @date 2021/2/5
 */
public class Demo06 {
    
    
    public static void main(String[] args) {
    
    
        Demo06Service service = new Demo06Service();
        Demo06ThreadA threadA = new Demo06ThreadA(service);
        Demo06ThreadB threadB = new Demo06ThreadB(service);
        threadA.start();
        threadB.start();
    }
}
class Demo06Service{
    
    
    @SneakyThrows
    public synchronized void methodOne(){
    
    
        System.out.println("methodOne方法执行");
        Thread.sleep(2000);
    }
    @SneakyThrows
    public synchronized void methodTwo(){
    
    
        System.out.println("methodTwo方法执行");
        Thread.sleep(2000);
    }
}
class Demo06ThreadA extends Thread{
    
    
    private Demo06Service service;
    public Demo06ThreadA(Demo06Service service){
    
    
        this.service = service;
    }
    @Override
    public void run() {
    
    
        service.methodOne();
    }
}class Demo06ThreadB extends Thread{
    
    
    private Demo06Service service;
    public Demo06ThreadB(Demo06Service service){
    
    
        this.service = service;
    }
    @Override
    public void run() {
    
    
        service.methodTwo();
    }
}

결론적으로:
여기에 사진 설명 삽입
여기에 사진 설명 삽입


더티 읽기

package com.hanyxx.thread;

import lombok.SneakyThrows;

/**
 * 模拟线程不同步导致脏读问题
 * @author layman
 * @date 2021/2/5
 */
public class Demo07 {
    
    
    public static void main(String[] args) throws InterruptedException {
    
    
        UserService service = new UserService();
        Demo07Thread thread = new Demo07Thread(service);
        thread.start();
        Thread.sleep(500);
        service.getValue();
    }
}
class UserService{
    
    
    private String username = "layman";
    private String password = "12345";

    @SneakyThrows
    public void change(String username, String password){
    
    
        this.username = username;
        Thread.sleep(1000);
        this.password = password;
        System.out.println("线程名称:["+Thread.currentThread().getName()+"] ,change方法执行,username: "+username+" ,password: "+password);
    }
    public void getValue(){
    
    
        System.out.println("线程名称:["+Thread.currentThread().getName()+"] ,getValue方法执行,username: "+username+" ,password: "+password);
    }
}
class Demo07Thread extends Thread{
    
    
    private UserService service;
    public Demo07Thread(UserService service){
    
    
        this.service = service;
    }
    @Override
    public void run() {
    
    
        service.change("菠菜饭团","54321");
    }
}

여기에 사진 설명 삽입

해결책:

  • change ()와 getValue ()는 모두 동기화되어 장식됩니다.
  • 여기에 사진 설명 삽입

재진입 잠금

  • 동기화 된 키워드에는 잠금 재진입 기능이 있습니다. 즉, 동기화를 사용할 때 스레드가 객체 잠금을 획득하고 객체를 다시 요청하면 객체 잠금을 다시 획득 할 수 있습니다.
  • 잠금 재진입은 부모-자식 클래스 상속을 지원합니다.
package com.hanyxx.thread;

/**
 * 测试锁重入以及父子继承
 * @author layman
 * @date 2021/2/5
 */
public class Demo08 {
    
    
    public static void main(String[] args) {
    
    
        new Demo08Thread().start();
    }
}
class Demo08Service{
    
    
    public synchronized  void methodA() {
    
    
        System.out.println("methodA方法执行了");
        methodB();
    }
    public synchronized  void methodB() {
    
    
        System.out.println("methodB方法执行了");
        methodC();
    }
    public synchronized  void methodC() {
    
    
        System.out.println("methodC方法执行了");
    }
}
//锁重入支持父子继承
class Demo08ServiceA extends Demo08Service{
    
    
    public synchronized  void methodD() {
    
    
        System.out.println("methodD方法执行了");
        methodA();
    }
}
class Demo08Thread extends  Thread{
    
    
    @Override
    public void run() {
    
    
        /*Demo08Service service = new Demo08Service();
        service.methodA();*/
        Demo08ServiceA service = new Demo08ServiceA();
        service.methodD();
    }
}

추신 : 예외로 인해 잠금이 해제됩니다.

추천

출처blog.csdn.net/single_0910/article/details/113701136