Are actions in the thread expected before the call ConcurrentMap.remove()— before actions after viewing the deletion from another thread?
ConcurrentMap.remove()
The documentation talks about objects placed in the collection:
Actions in a stream before placing an object in any parallel collection will occur - before actions after accessing or removing this element from the collection in another stream.
Code example:
{ final ConcurrentMap map = new ConcurrentHashMap(); map.put(1, new Object()); final int[] value = { 0 }; new Thread(() -> { value[0]++; value[0]++; value[0]++; value[0]++; value[0]++; map.remove(1); // A }).start(); new Thread(() -> { if (map.get(1) == null) { // B System.out.println(value[0]); // expect 5 } }).start(); }
Is A in the case where a connection occurs with B? Therefore, if the program only, if ever, prints 5?
concurrency , .
-, null, , , , , - , , , , .
null
, , , , , null, , . , , , , . .
: map.put(1, new Object()) , , null 1, , , , .
map.put(1, new Object())
1
Java 8s ConcurrentHashMap :
ConcurrentHashMap
Retrievals , . ( "" ( ) , .)
null.
, (Java 8) ConcurrentHashMap , , volatile . , , , , .
volatile
ConcurrentHashMap - , map.remove(1) , . map.get(1) , .
map.remove(1)
map.get(1)
ConcurrentHashMap Java 7 , / .
ConcurrentSkipListMap , .
, , .
, .
put() get() . . - get() get() remove(), put() put().
put()
get()
remove()
. . -. , get() , .
Intel , . Java , , .
A B.
put. , B , A.
put
++ remove . volatile ; Map , . .
++
remove
To my understanding, A could delete and be written back, then the last ++ will happen, and something like 4 will be printed in B. I would add volatileto the array. The card itself will be fine.
I am far from certain, but since I did not see the corresponding answer, I close my neck. (To get to know yourself.)
Source: https://habr.com/ru/post/1653743/More articles:How to assign $ http.get response to AngularJS variable - javascriptCreating logic inside Firebase - serverMeaning of Observation - OpenAI Gym - pythonHow to prevent redeployment of a trawl? - continuous-integrationAdd item to Seq in scala - scalaJavascript Bitwise Operation - javascriptIt always starts for the purpose of "everything", even if nothing is updated - c ++Extract strip data from webhook lane event - javaWin32 and catch WM_SIZE message outside WindowProcedure - c ++Bar chart with log y axis in Julia Plots.jl - julia-langAll Articles