Hi,
My Data class has a static integer which I use to keep track of the number of record currently in the db. It is declared thus:
private static int recordCount = -1
I need to ensure that *only* the first
thread that accesses the database initialises it to the number of records in the DB at startup. My question is whether it is better to synchronise access to this variable like this:
or whether this is better:
The advantage of the first approach is that it is definitely threadsafe, but it requires every thread to get the lock on Data.getClass() - even if recordCount has already been initialised. The second approach doesn't require this, but it looks like recordCount *could* be initialised twice.
Perhaps the best approach is a combination of the two:
But this looks a bit strange to me. Thanks in advance for your comments!
Cheers,
Dan