I tried to search, but could not find the exact answer I was looking for, so I asked a new question.
If you want to share any mutable objects (objects) between multiple threads, are there any recommendations / principles / recommendations for this?
Or will it just change in each case?
Sharing mutable objects between threads is risky.
The safest way is to make objects immutable, you can freely share them.
, , . (synchronized, AtomixX ..).
synchronized
AtomixX
, , .
java , / , .
:
/, , , .
, . , .
, .
. , . , /api, , . Scala , Java .
, . , . , , . , .
, . . . , Akka, .
, incrementAndGet. .
Java api , .
. , dsl. , , . , synchronize .
. StringBuilder , synchronized (builder) . .
synchronized (builder)
(, StringBuffer), .
public static void main(String[] args) throws InterruptedException { StringBuilder builder = new StringBuilder(""); Thread one = new Thread() { public void run() { for (int i = 0; i < 1000; i++) { //synchronized (builder) { builder.append("thread one\n"); //} } } }; Thread two = new Thread() { public void run() { for (int i = 0; i < 1000; i++) { //synchronized (builder) { builder.append("thread two\n"); //} } } }; one.start(); two.start(); one.join(); two.join(); System.out.println(builder); }
, , Java Concurrency 3 - .
.
:;( ) ;, .
, :
, . :;volatile AtomicReference;;, .
, . :
, / synchronize.
Source: https://habr.com/ru/post/1671033/More articles:Click event on tile sheet map in Shiny - rAdd / remove dynamic DOM elements with Vue - vue.jshttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1671030/string-literals-to-number-at-compile-time&usg=ALkJrhjQmzbB9eWAYnX2J6ILhVLC-gHa1gHow to remove last name from path in VBA - vbaHandling a strange problem with float - processingIgnore empty elements from json body using jackson - javaJackson Misc. JSONFilter of the same class - javaERROR in Failed to resolve the module @ angular / router / src / router_config_loader - angularJavascript Loop through и Concatenate 2 массива - javascriptПеремещение Mixin в Extjs - mixinsAll Articles