在CompletableFuture回调中为类成员属性设置值是否是线程安全的?

时间:2018-06-26 18:17:46

标签: java multithreading java-8 thread-safety completable-future

我是Java 8中的tap的新手,我想知道当我将结果设置为回调中的类成员属性并尝试在{{1之后读取它们时,以下代码片段是否是线程安全的? }},为什么?

CompletableFuture

1 个答案:

答案 0 :(得分:1)

[ { "employees": [ { "jobFamilyLevel": "spm", "addresses": [ { "city": "Tioga", "country": "France, Metropolitan", "zipCode": 8655, "line1": "229", "line2": "Kenmore Terrace", "landmark": "Delaware", "type": "<SyntaxError: Invalid or unexpected token>" } ], "id": "e5b4cdfc-cfe5-4dcc-bd4a-813c068dc61c", "designation": "Simon", "name": "Norman" }, { "jobFamilyLevel": "lead", "addresses": [ { "city": "Marienthal", "country": "Cote D'Ivoire (Ivory Coast)", "zipCode": 1123, "line1": "660", "line2": "Withers Street", "landmark": "Alaska", "type": "<SyntaxError: Invalid or unexpected token>" } ], "id": "8dfe3cd5-a457-4810-8c61-8fb0fc09613d", "designation": "Baldwin", "name": "England" }, { "jobFamilyLevel": "avp", "addresses": [ { "city": "Calverton", "country": "Mauritius", "zipCode": 8144, "line1": "460", "line2": "Monroe Street", "landmark": "Arizona", "type": "<SyntaxError: Invalid or unexpected token>" } ], "id": "bb34a0aa-823a-40e6-9e68-5b8db9bb269a", "designation": "Hawkins", "name": "Wooten" }, { "jobFamilyLevel": "lead", "addresses": [ { "city": "Juntura", "country": "Malaysia", "zipCode": 6173, "line1": "710", "line2": "Matthews Place", "landmark": "South Dakota", "type": "<SyntaxError: Invalid or unexpected token>" } ], "id": "685c09e9-ec4c-4f38-b5dd-2e1b9d098543", "designation": "Short", "name": "Roach" } ], "name": "Patsy", "description": "abc" }, { "employees": [ { "jobFamilyLevel": "avp", "addresses": [ { "city": "Trinway", "country": "Chad", "zipCode": 6640, "line1": "180", "line2": "Bleecker Street", "landmark": "Guam", "type": "<SyntaxError: Invalid or unexpected token>" } ], "id": "c7c1dfb3-2c19-4a16-aadb-2161cbba5034", "designation": "Ballard", "name": "Stuart" }, { "jobFamilyLevel": "lead", "addresses": [ { "city": "Riviera", "country": "Belarus", "zipCode": 6868, "line1": "604", "line2": "Abbey Court", "landmark": "Rhode Island", "type": "<SyntaxError: Invalid or unexpected token>" } ], "id": "3e0a6192-5bd5-4ffc-92ef-21882c7a992d", "designation": "Hoffman", "name": "Stone" }, { "jobFamilyLevel": "trainee", "addresses": [ { "city": "Elwood", "country": "Paraguay", "zipCode": 3457, "line1": "822", "line2": "Greenwood Avenue", "landmark": "Palau", "type": "<SyntaxError: Invalid or unexpected token>" } ], "id": "c28c8a4f-ccfb-405f-98f9-bb5b6602939a", "designation": "Chase", "name": "Craft" }, { "jobFamilyLevel": "pm", "addresses": [ { "city": "Cliffside", "country": "Gambia", "zipCode": 5404, "line1": "534", "line2": "Eagle Street", "landmark": "Montana", "type": "<SyntaxError: Invalid or unexpected token>" } ], "id": "996e0c9e-73e7-4d41-9c43-8eff4dbe0d6d", "designation": "Ray", "name": "Meyers" } ], "name": "Mable", "description": "abc" } ] 软件包中定义的Memory Consistency Properties涵盖了您的问题:

  

Chapter 17 of the Java Language Specification定义了   内存操作(例如共享变量的读取和写入)上的 happens-before 关系。 一个线程写入的结果是   仅在写入时保证对另一个线程的读取可见   操作发生在之前。 […]

     

SELECT * FROM organization where ?::text IN (SELECT jsonb_array_elements(jsonb_array_elements(info -> 'departments') -> 'employees') ->> 'jobFamilyLevel') 中所有类的方法及其   子程序包将这些保证扩展到更高级别的同步。   特别是:

     
      
  • […]
  •   
  • java.util.concurrent happen-before 动作代表的异步计算所采取的动作,这些动作是在检索到   通过java.util.concurrent在另一个线程中得到结果。
  •   
  • […]
  •   

因此,总而言之,您的Future调用保证了在线程执行之后,您的写入将对线程可见。