77
88import java .util .concurrent .atomic .AtomicInteger ;
99import java .util .concurrent .atomic .AtomicIntegerArray ;
10+ import java .util .concurrent .atomic .AtomicIntegerFieldUpdater ;
1011import java .util .concurrent .atomic .AtomicReference ;
1112
1213/**
@@ -17,13 +18,15 @@ public class AtomFactoryTest extends TestCase {
1718 AtomicInteger integer ;
1819 AtomicIntegerArray array ;
1920 AtomicReference <MyObject > reference ;
21+ AtomicIntegerFieldUpdater <MyObject > updater ;
2022
2123 public void setUp () throws Exception {
2224 super .setUp ();
2325 integer = AtomFactory .getInstance ().createAtomInt (1 );
2426 }
2527
2628 public void testAtomInt () throws Exception {
29+ System .out .println ("int原子类" );
2730 new Thread (()->{
2831 for (int i = 0 ; i < 10 ; i ++) {
2932 integer .getAndIncrement ();
@@ -42,6 +45,7 @@ public void testAtomInt() throws Exception {
4245 }
4346
4447 public void testAtomArray () throws Exception {
48+ System .out .println ("原子类数组" );
4549 int [] value = new int []{1 ,2 ,3 ,4 };
4650 array = AtomFactory .getInstance ().createAtomArray (value );
4751 array .getAndSet (1 ,10 );
@@ -50,14 +54,23 @@ public void testAtomArray() throws Exception {
5054 }
5155
5256 public void testAtomRef ()throws Exception {
57+ System .out .println ("原子类" );
5358 MyObject object = new MyObject ();
5459 reference = AtomFactory .getInstance ().createAtomReference (object );
5560 reference .set (object );
5661 MyObject newObject = new MyObject ();
5762 newObject .name = "xiaoli" ;
5863 reference .compareAndSet (object , newObject );
5964 System .out .println (reference .get ().name );
65+ }
66+
6067
68+ public void testUpdater () throws Exception {
69+ System .out .println ("原子类更新字段" );
70+ updater = AtomFactory .getInstance ().createAtomIntegerUpdate ("id" );
71+ MyObject object = new MyObject ();
72+ System .out .println (updater .getAndIncrement (object ));
73+ System .out .println (updater .get (object ));
6174 }
6275
6376
0 commit comments