Bob Hanson wrote:
> Nicolas Vervelle wrote:
>
>> Bob Hanson wrote:
>>  
>>
>>> pps. The following is more elegant and also works:
>>>
>>>  static Boolean[] testInit = new Boolean[1];
>>>  private void initialize() {
>>>    if (testInit[0] != null) {
>>>      while (!testInit[0].booleanValue()) {
>>>        try{
>>>          Thread.sleep(10);
>>>        } catch(Exception e) {
>>>          System.out.println("geodesic constructor error "+e);
>>>        }
>>>      }
>>>      return;
>>>    }
>>>    testInit[0] = Boolean.FALSE;
>>> ...
>>>    System.out.println("geodesic initialization completed");
>>>    testInit[0] = Boolean.TRUE;
>>>  }
>>>
>>> It takes advantage of the fact that static testInit[0] has THREE 
>>> possible values: null, false, and true.
>>> This is what I'm going with for now.
>>>  
>>>   
>>
>> That won't work in every situation :
>> - thread 1 executes "if (testInit[0] != null)" and finds that it 
>> evals to false
>>  
>>
> No, you are wrong, I think Thread 1 evaluates it as "null". 
> guaranteed. That's a "Boolean" not "boolean".

That's what I was saying: testInit[0] = null so the != evals to false

At the beginning of the program, testInit[0] = null
The first thread that calls initialize() evals (testInit[0] != null) to 
false, so it goes on with testInit[0] = Boolean.TRUE
The problem is that the test + assignment is not an atomic operation so 
before the assignment is done, an other thread can call the same test 
and also get a false, so it goes on with testInit[0] = Boolean.TRUE

Really, check for the Singleton pattern and how it can be correctly 
handled for multi-threaded environment.
It's not simple, and it really requires some synchronization at one point







_______________________________________________
Jmol-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jmol-developers

Reply via email to