KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > FutureObject


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 /**
22  ** A multiprocessor-safe wrapper to defer evaluation of an object until it
23  ** is needed.
24  **/

25 public abstract class FutureObject
26 {
27     private static final boolean _volatileIsEffectiveMemoryBarrier = MemoryBarrier.USE_VOLATILE;
28
29     private volatile boolean _evaluated = false;
30
31     private Object _value;
32
33     /**
34      ** Sub-classes should override this method to get the object's value
35      ** when it is first needed.
36      **/

37     public abstract Object evaluate();
38
39     public final Object getValue()
40     {
41         if (_volatileIsEffectiveMemoryBarrier)
42         {
43             if (! _evaluated)
44             {
45                 synchronized (this)
46                 {
47                     if (! _evaluated)
48                     {
49                         _value = evaluate();
50                         _evaluated = true;
51                     }
52                 }
53             }
54         }
55         else
56         {
57             synchronized (this)
58             {
59                 if (! _evaluated)
60                 {
61                     _value = evaluate();
62                     _evaluated = true;
63                 }
64             }
65         }
66         return _value;
67     }
68 }
69
Popular Tags