Another question about thread locks

The contention of shared resources is solved through the lock mechanism between multiple threads. Usually, we will use a specific object as a lock. Then, if the value of the lock changes? Will there still be synchronization between threads?

Example code:

import java.util.Objects;

public class Operator {
    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Operator operator = (Operator) o;
        return Objects.equals(id, operator.id) &&
                Objects.equals(name, operator.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }
}

This is a very normal Java class.

Next is the service that operates on this class:

public class OperatorService {

    public void updateName(Operator operator) {
        synchronized(operator) {
            System.out.println("线程 :" + Thread.currentThread().getName() + " 进入");
            System.out.println("operator对象name属性值:" + operator.getName());
            operator.setName("test" + Math.random());
            System.out.println("operator对象name属性值已更改:" + operator.getName());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "离开");
        }
    }
}

Main run entry:

public class Main {
    public static void main(String[] args) throws Exception {
        Operator operator = new Operator();
        operator.setId("123");
        operator.setName("123");

        OperatorService service = new OperatorService();

        new Thread(new ThreadA(operator, service)).start();
        new Thread(new ThreadB(operator, service)).start();

    }

    static class ThreadA implements Runnable{
        private Operator operator;
        private OperatorService service;

        public ThreadA(Operator operator, OperatorService service) {
            this.service = service;
            this.operator = operator;
        }

        @Override
        public void run() {
            service.updateName(operator);
        }
    }

    static class ThreadB implements Runnable{
        private Operator operator;
        private OperatorService service;

        public ThreadB(Operator operator, OperatorService service) {
            this.service = service;
            this.operator = operator;
        }

        @Override
        public void run() {
            service.updateName(operator);
        }
    }
}

What would the output here look like? The output is as follows:

线程 :Thread-0 进入
operator对象name属性值:123
operator对象name属性值已更改:test0.49531243439875083
Thread-0离开
线程 :Thread-1 进入
operator对象name属性值:test0.49531243439875083
operator对象name属性值已更改:test0.8638112164864292
Thread-1离开

Although the thread changes the value of the lock, the two threads are still synchronized.

Summary: As long as the thread lock object remains unchanged and the value of the lock object does not change, it does not affect the synchronization effect between threads.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325309709&siteId=291194637