Java – what is the difference between atomicreference and synchronized?

What is the difference between atomicreference and synchronized?

public class Internet {
    AtomicReference<String> address;
    public String getAddress(){
        return address.toString();
    }
    public void setAddress(String address) {
        this.address.set(address);
    }

}

I pass the class to some threads trying to use the class at the same time. If I use it, it is the same thing:

public class Internet {
    String address;
    public String getAddress(){
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

Then access the class before the thread uses synchronized?

Solution

You did not initialize the reference in the first example, it might be:

public class Internet {
    AtomicReference<String> address = new AtomicReference<String>();
    public String getAddress(){
        String s = address.get();
        return s == null ? null : s.toString();
    }
    public void setAddress(String address) {
        this.address.set(address);
    }
}

The location of access restrictions is important If you put the control in the accessed object, it can control its invariants independently, which is much more fragile than relying on the thread to correctly synchronize all. One of the misbehaving access threads may destroy the accessed object So the first example is much better on this account

If you change the second example so that the object can control its own locking (so it does not rely on the thread accessing it to do so safely), it is as follows:

public class Internet {
    private final Object lock = new Object();
    private String s;
    public String getAddress() {
       synchronized(lock) {
           return s;
       }
    }
    public void setAddress(String s) {
        synchronized(lock) {
            this.s = s;
        }
    }
}

Then it is a closer comparison, one dependent on locking and the other on atomic references The one who uses atomicreference tries to use machine - level atomic processing instructions to avoid locking Which is faster may depend on your hardware and JVM and the processing load. Generally, atomic methods should be faster Synchronization method is a more general mechanism; With synchronized blocks, you can more easily group multiple assignments together, where atomic references are more complex

As James says in his answer, your thread is waiting for locking; Deadlock is possible without timeout With atomic references, threads make changes without waiting for shared locks

The simplest and best performing way to achieve this is to organize the code so that objects can be made immutable, so that all locking, busy waiting and cache updates can be avoided:

public final class Internet {
    private final String s;
    public Internet(String s) {
        this.s = s;
    }
    public String getAddress() {return s;}
}

In descending order of priority:

>Choose invariance whenever possible. > For immutable code, try to limit variation to one thread. > If you only need to make one change between threads, use the atomic method. > Use locking if multiple changes across threads need to occur without interference from other threads

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>