If there is no contention, is synchronization required on reads


Petro Semeniuk:

Consider the following code sniper:

package sync;

public class LockQuestion {
    private String mutable;

    public synchronized void setMutable(String mutable) {
        this.mutable = mutable;
    }

    public String getMutable() {
        return mutable;
    }   
}

At time 1, thread Thread1 will update the "mutable" variable. Synchronization is required in the setter in order to flush memory from local cache to main memory. At time Time2 (time2 > time1, no thread contention), thread Thread2 will read the mutable value.

The question is - do I need to synchronize before getting the method? It looks like this shouldn't cause any problems - the memory should be up to date and Thread1 should invalidate and update Thread2's local cache, but I'm not sure.

anonymous:

Not surprisingly, why not just use atomic references from java.util.concurrent ?

(For what it's worth, my reading of " before-before" doesn't guarantee that Thread2 will see mutable changes unless it also uses synchronous... but I always have a headache with this part of the JLS, so Please use atomic references)

Related


Understand synchronization methods at runtime without contention

San Antonio: java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode) These are for learning purposes and are not intended to be used in production. I am trying to understa

Understand synchronization methods at runtime without contention

San Antonio: java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode) These are for learning purposes and are not intended to be used in production. I am trying to understa

Understand synchronization methods at runtime without contention

San Antonio: java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode) These are for learning purposes and are not intended to be used in production. I am trying to understa

What does "fast path" contention-free synchronization mean?

Geeks: From the " Performance and Scalability" chapter of the JCIP book : The synchronization mechanism is optimized for contention-free (volatility is always contention-free), and as of this writing, the performance cost of "fast-path" contention-free synchro

What does "fast path" contention-free synchronization mean?

Geeks: From the " Performance and Scalability" chapter of the JCIP book : The synchronization mechanism is optimized for contention-free (volatility is always contention-free), and as of this writing, the performance cost of "fast-path" contention-free synchro

With ConcurrentHashMap, when is synchronization required?

For each position: I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initial

With ConcurrentHashMap, when is synchronization required?

For each position: I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initial

Is synchronization with multiprocessor required in python?

Bobo When using code like this def execute_run(list_out): ... do something pool = ThreadPoolExecutor(6) for i in list1: for j in list2: pool.submit(myfunc, list_out) pool.join() Assuming threads modify list_out, do they operate in a sync

Synchronization is required in getters and setters

snow leopard Probably a very stupid question. Just wanted to confirm my understanding. class Test { private volatile String id; public void setID(String id) { this.id = id; } public String getID() {

Synchronization is required in asynchronous code

Cyrillic I'm trying to create a factory for an angular module that returns a JSON object received through angular $http.get(). In the for's callback function success(), I am trying to assign an object to a variable products. It seems to productsbe returned fro

Synchronization is required in getters and setters

snow leopard Probably a very stupid question. Just wanted to confirm my understanding. class Test { private volatile String id; public void setID(String id) { this.id = id; } public String getID() {

Is synchronization with multiprocessor required in python?

Bobo When using code like this def execute_run(list_out): ... do something pool = ThreadPoolExecutor(6) for i in list1: for j in list2: pool.submit(myfunc, list_out) pool.join() Assuming threads modify list_out, do they operate in a sync

With ConcurrentHashMap, when is synchronization required?

For each position: I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initial

Synchronization is required in getters and setters

snow leopard Probably a very stupid question. Just wanted to confirm my understanding. class Test { private volatile String id; public void setID(String id) { this.id = id; } public String getID() {

Only one thread is required for synchronization

Chapter 135 If I have a class that extends Thread with static methods (this is very simplified): public class MyThread extends Thread { private static long SLEEP_INT = 30000; private static Map<Integer, String> myData; //every 30 seconds, update

read always reads less than required octets

Adrian For homework I have to create a client/server program to store data. The client reads the file and sends it over the socket to the server, which receives the file and stores the data. The ultimate goal is to implement sharding across multiple servers. T

When using synchronization, atomic references are not required

Maxim Dmitriev The code is from the Java Concurrency Guidelines by Fred Long . I understand that a set of atomic operations is not atomic. Therefore, the following code does not meet the requirements. To find the code, see page 23. public class Adder { pr

When using synchronization, atomic references are not required

Maxim Dmitriev The code is from the Java Concurrency Guidelines by Fred Long . I understand that a set of atomic operations is not atomic. Therefore, the following code does not meet the requirements. To find the code, see page 23. public class Adder { pr