Another problem about thread lock

The contention for 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 threads still be synchronized?

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 common Java class.

Next is the service for this class operation:

public class OperatorService {

    public void updateName(Operator operator) {
        synchronized(operator) {
            System.out.println("Threading:" + Thread.currentThread().getName() + " Get into");
            System.out.println("operator object name Attribute value:" + operator.getName());
            operator.setName("test" + Math.random());
            System.out.println("operator object name Property value changed:" + operator.getName());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "leave");
        }
    }
}

Main operation entrance:

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 is the output here? The output results are as follows:

Thread: Thread-0 enters
 operator object name attribute value: 123
 operator object name property value changed: test0.49531243439875083
 Thread-0 leave
 Thread: Thread-1 in
 operator object name attribute value: test0.49531243439875083
 operator object name property value changed: test0.8638112164864292
 Thread-1 leave

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 remains unchanged, the synchronization effect between threads will not be affected.

Tags: Attribute Java

Posted on Fri, 20 Mar 2020 08:23:14 -0700 by phertzog