@@ -268,7 +268,121 @@ class Person {
268
268
Daisy
269
269
20
270
270
```
271
+ #### 4.3 AtomicStampedReference 类使用示例
271
272
273
+ ``` java
274
+ import java.util.concurrent.atomic.AtomicStampedReference ;
275
+
276
+ public class AtomicStampedReferenceDemo {
277
+ public static void main (String [] args ) {
278
+ // 实例化、取当前值和 stamp 值
279
+ final Integer initialRef = 0 , initialStamp = 0 ;
280
+ final AtomicStampedReference<Integer > asr = new AtomicStampedReference<> (initialRef, initialStamp);
281
+ System . out. println(" currentValue=" + asr. getReference() + " , currentStamp=" + asr. getStamp());
282
+
283
+ // compare and set
284
+ final Integer newReference = 666 , newStamp = 999 ;
285
+ final boolean casResult = asr. compareAndSet(initialRef, newReference, initialStamp, newStamp);
286
+ System . out. println(" currentValue=" + asr. getReference()
287
+ + " , currentStamp=" + asr. getStamp()
288
+ + " , casResult=" + casResult);
289
+
290
+ // 获取当前的值和当前的 stamp 值
291
+ int [] arr = new int [1 ];
292
+ final Integer currentValue = asr. get(arr);
293
+ final int currentStamp = arr[0 ];
294
+ System . out. println(" currentValue=" + currentValue + " , currentStamp=" + currentStamp);
295
+
296
+ // 单独设置 stamp 值
297
+ final boolean attemptStampResult = asr. attemptStamp(newReference, 88 );
298
+ System . out. println(" currentValue=" + asr. getReference()
299
+ + " , currentStamp=" + asr. getStamp()
300
+ + " , attemptStampResult=" + attemptStampResult);
301
+
302
+ // 重新设置当前值和 stamp 值
303
+ asr. set(initialRef, initialStamp);
304
+ System . out. println(" currentValue=" + asr. getReference() + " , currentStamp=" + asr. getStamp());
305
+
306
+ // [不推荐使用,除非搞清楚注释的意思了] weak compare and set
307
+ // 困惑!weakCompareAndSet 这个方法最终还是调用 compareAndSet 方法。[版本: jdk-8u191]
308
+ // 但是注释上写着 "May fail spuriously and does not provide ordering guarantees,
309
+ // so is only rarely an appropriate alternative to compareAndSet."
310
+ // todo 感觉有可能是 jvm 通过方法名在 native 方法里面做了转发
311
+ final boolean wCasResult = asr. weakCompareAndSet(initialRef, newReference, initialStamp, newStamp);
312
+ System . out. println(" currentValue=" + asr. getReference()
313
+ + " , currentStamp=" + asr. getStamp()
314
+ + " , wCasResult=" + wCasResult);
315
+ }
316
+ }
317
+ ```
318
+
319
+ 输出结果如下:
320
+ ```
321
+ currentValue=0, currentStamp=0
322
+ currentValue=666, currentStamp=999, casResult=true
323
+ currentValue=666, currentStamp=999
324
+ currentValue=666, currentStamp=88, attemptStampResult=true
325
+ currentValue=0, currentStamp=0
326
+ currentValue=666, currentStamp=999, wCasResult=true
327
+ ```
328
+
329
+ #### 4.4 AtomicStampedReference 类使用示例
330
+
331
+ ``` java
332
+ import java.util.concurrent.atomic.AtomicMarkableReference ;
333
+
334
+ public class AtomicMarkableReferenceDemo {
335
+ public static void main (String [] args ) {
336
+ // 实例化、取当前值和 mark 值
337
+ final Boolean initialRef = null , initialMark = false ;
338
+ final AtomicMarkableReference<Boolean > amr = new AtomicMarkableReference<> (initialRef, initialMark);
339
+ System . out. println(" currentValue=" + amr. getReference() + " , currentMark=" + amr. isMarked());
340
+
341
+ // compare and set
342
+ final Boolean newReference1 = true , newMark1 = true ;
343
+ final boolean casResult = amr. compareAndSet(initialRef, newReference1, initialMark, newMark1);
344
+ System . out. println(" currentValue=" + amr. getReference()
345
+ + " , currentMark=" + amr. isMarked()
346
+ + " , casResult=" + casResult);
347
+
348
+ // 获取当前的值和当前的 mark 值
349
+ boolean [] arr = new boolean [1 ];
350
+ final Boolean currentValue = amr. get(arr);
351
+ final boolean currentMark = arr[0 ];
352
+ System . out. println(" currentValue=" + currentValue + " , currentMark=" + currentMark);
353
+
354
+ // 单独设置 mark 值
355
+ final boolean attemptMarkResult = amr. attemptMark(newReference1, false );
356
+ System . out. println(" currentValue=" + amr. getReference()
357
+ + " , currentMark=" + amr. isMarked()
358
+ + " , attemptMarkResult=" + attemptMarkResult);
359
+
360
+ // 重新设置当前值和 mark 值
361
+ amr. set(initialRef, initialMark);
362
+ System . out. println(" currentValue=" + amr. getReference() + " , currentMark=" + amr. isMarked());
363
+
364
+ // [不推荐使用,除非搞清楚注释的意思了] weak compare and set
365
+ // 困惑!weakCompareAndSet 这个方法最终还是调用 compareAndSet 方法。[版本: jdk-8u191]
366
+ // 但是注释上写着 "May fail spuriously and does not provide ordering guarantees,
367
+ // so is only rarely an appropriate alternative to compareAndSet."
368
+ // todo 感觉有可能是 jvm 通过方法名在 native 方法里面做了转发
369
+ final boolean wCasResult = amr. weakCompareAndSet(initialRef, newReference1, initialMark, newMark1);
370
+ System . out. println(" currentValue=" + amr. getReference()
371
+ + " , currentMark=" + amr. isMarked()
372
+ + " , wCasResult=" + wCasResult);
373
+ }
374
+ }
375
+ ```
376
+
377
+ 输出结果如下:
378
+ ```
379
+ currentValue=null, currentMark=false
380
+ currentValue=true, currentMark=true, casResult=true
381
+ currentValue=true, currentMark=true
382
+ currentValue=true, currentMark=false, attemptMarkResult=true
383
+ currentValue=null, currentMark=false
384
+ currentValue=true, currentMark=true, wCasResult=true
385
+ ```
272
386
273
387
### 5 对象的属性修改类型原子类
274
388
0 commit comments