1 /*
2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 6275329
27  * @summary lazySet methods
28  */
29 
30 import java.util.concurrent.atomic.AtomicBoolean;
31 import java.util.concurrent.atomic.AtomicInteger;
32 import java.util.concurrent.atomic.AtomicIntegerArray;
33 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
34 import java.util.concurrent.atomic.AtomicLong;
35 import java.util.concurrent.atomic.AtomicLongArray;
36 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
37 import java.util.concurrent.atomic.AtomicReference;
38 import java.util.concurrent.atomic.AtomicReferenceArray;
39 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
40 
41 public class Lazy {
42     volatile int ii;
43     volatile long ll;
44     volatile Boolean bb;
45     static final Lazy z = new Lazy();
46 
main(String[] args)47     public static void main(String[] args) throws Exception {
48         final AtomicBoolean b = new AtomicBoolean();
49         final AtomicInteger i = new AtomicInteger();
50         final AtomicLong    l = new AtomicLong();
51         final AtomicReference<Long> r = new AtomicReference<>();
52 
53         final AtomicIntegerArray ia = new AtomicIntegerArray(1);
54         final AtomicLongArray    la = new AtomicLongArray(1);
55         final AtomicReferenceArray<Long> ra = new AtomicReferenceArray<>(1);
56 
57         final AtomicIntegerFieldUpdater<Lazy> iu =
58             AtomicIntegerFieldUpdater.newUpdater(Lazy.class, "ii");
59         final AtomicLongFieldUpdater<Lazy> lu =
60             AtomicLongFieldUpdater.newUpdater(Lazy.class, "ll");
61         final AtomicReferenceFieldUpdater<Lazy,Boolean> ru =
62             AtomicReferenceFieldUpdater.newUpdater(Lazy.class,
63                                                    Boolean.class, "bb");
64 
65         Thread[] threads = {
66             new Thread() { public void run() { b.lazySet(true);    }},
67             new Thread() { public void run() { i.lazySet(2);       }},
68             new Thread() { public void run() { l.lazySet(3L);      }},
69             new Thread() { public void run() { r.lazySet(9L);      }},
70             new Thread() { public void run() { ia.lazySet(0,4);    }},
71             new Thread() { public void run() { la.lazySet(0,5L);   }},
72             new Thread() { public void run() { ra.lazySet(0,6L);   }},
73             new Thread() { public void run() { iu.lazySet(z,7);    }},
74             new Thread() { public void run() { lu.lazySet(z,8L);   }},
75             new Thread() { public void run() { ru.lazySet(z,true); }}};
76 
77         for (Thread t : threads) t.start();
78         for (Thread t : threads) t.join();
79 
80         if (! (b.get()   == true &&
81                i.get()   == 2    &&
82                l.get()   == 3L   &&
83                r.get()   == 9L   &&
84                ia.get(0) == 4    &&
85                la.get(0) == 5L   &&
86                ra.get(0) == 6L   &&
87                z.ii      == 7    &&
88                z.ll      == 8L   &&
89                z.bb      == true))
90             throw new Exception("lazySet failed");
91     }
92 }
93