String and concurrency in Java

This may be a related question: Java assignment issues - is it atomic?

I have the same class as the OP that acts on a mutable string reference. But recruitment rarely happens. (basically this line is part of the server configuration, which only reboots when forced).

public class Test { private String s; public void setS(String str){ s = str; } public String getS(){ return s; } } 

Several threads will beat this variable to read its value. What is the best way to make it “safe” without requiring performance degradation by declaring it unstable?

I'm currently heading towards ReadWriteLock, but as far as I understand, ReadWrite locks don't make it safe from thread caching? if some kind of synchronization doesn't happen? What does it mean that I returned the full circle back to use only the volatile keyword?

Do I understand correctly? There is nothing that could “notify” other threads about a variable update in the main memory manually, so that they can update their local cache only once on the full moon?

volatile on this seems redundant, given that the server application is designed to work for several months without a reboot. By then, that would have served several million readings. I think I could just set String as a static final and not let it mutate without a full application and restarting the JVM.

+4
source share
4 answers

Reading and writing to links are atomic. The problems that may arise are an attempt to read and write (update) or to ensure that after writing all the streams, see This change on the next read. However, only you can tell what your requirements are.

Using volatile requires reading or writing a coherent copy of the cache. This does not require a copy to be made to / from main memory, as caches exchange among themselves, even between sockets. Performance impact, but this does not mean that caches are not used.

Even if access has reached the main memory, you can still do millions of calls per second.

+2
source

Why a variable string? Why not a Config class with a simple static string. When the configuration is updated, you change this static link, which is an atomic operation and will not be a problem for reading streams. You then have no synchronization, no penalties.

0
source

To notify clients on this server, you can use the observer template that is ever interested in receiving information about updating the server, can register for your event, and the server will send a notification. This should not be a bottleneck as you mentioned that rebooting is not often.

Now, to make this thread safe, you can have a separate thread that processes server status updates, and if you get a status check, if the status is "Update", you wait for it to complete, say, you went to bed. After the update stream is completed, it should change the state from “Update” to “Updated”, as soon as you exit the status check if it is “Update”, then go to sleep mode or start servicing the request.

This approach will add extra if in your code, but then it will allow you to reload the cache without forcibly restarting the application.

Also, this should not be a bottleneck, since updating the server is not frequent.

Hope this makes sense.

0
source

To avoid the volatile keyword, you can add a "memory protection method to the Test class, which is very rarely called, for example

 public synchronized void sync() { } 

This will force the thread to reread the field value from main memory.

In addition, you will need to change the installer to

  public synchronized void setS(String str){ s = str; } 

The synchronized will cause the tuning stream to write directly to main memory.

See here for a detailed explanation of timing and memory limitations.

0
source

Source: https://habr.com/ru/post/1383024/


All Articles