From: Miguel <mi...@jm...> - 2006-06-11 13:33:24
|
Nico wrote: > Bob Hanson wrote: >> pps. The following is more elegant and also works: >> >> static Boolean=5B=5D testInit =3D new Boolean=5B1=5D; >> private void initialize() =7B >> if (testInit=5B0=5D =21=3D null) =7B =5Bsnip=5D >> It takes advantage of the fact that static testInit=5B0=5D 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 =22if (testInit=5B0=5D =21=3D null)=22 and finds th= at it evals > to false > - thread 2 executes =22if (testInit=5B1=5D =21=3D null)=22 and finds th= at it evals > to false also > - thread 1 goes on > - thread 2 goes on > > You could try using a synchronization on a variable. Nico is correct. It is not possible to =22roll your own=22 synchronization in a high level= language without having some type of atomic 'test-and-set' operation support at a lower level. Essentially all CPUs for the last 50 years have had this, but popular high-level languages have not exposed this functionality. Java is arguabl= y the first commercially-successful-and-broadly-accepted language that incorporates synchronization primitives into the language. Per Nico's other message, my bug with 'initialize' is that it was marked 'synchronized' instead of 'syncronized static'. Miguel |