Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

locking style

 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan,

Personally I would prefer the first one. I understand your objections against this one, but it is by far the most clear solution and therefore IMHO the most preferrable.

Frans.
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static synchronized Data getInstance?
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ta Ri Ki Sun:
public static synchronized Data getInstance?

Smart thinking Ta!

Basically this is the same as Dan's first example. Although it has the same drawback (a thread that calls that method has to take the synchronization penalty, possibly to find that it does not need to do anything), I think it is much cleaner to make it a method. Making it a static method allows Dan to get rid of the Data.getClass(), which might look a bit dazzling to a novice programmer.

Frans.
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW Dan,

Ta's reply set me to think on what you want to achieve: you have a some functionality that you only want your first Data instance to execute. Now that sounds like singleton behavior. But apparently you want to allow multiple instances of Data?

Perhaps it is a good idea to move the part of the functionality that only the first instance should execute to a new class and make that class a singleton. Then the Data class only needs to get the singleton instance reference and does not have to worry about if it is (or is not) the first Data instance.

Frans.
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frans Janssen:

Smart thinking Ta!

Thanks
But if I understand correctly all he really needs is a static block
 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought about using a static block, but I rely on the non-static methods of the first created Data instance in order to read the number of records in the file, so I don't think I can use that approach.

Similarly, I don't think I can use the singleton approach, because I would rely on the first created instance of Data to pass the location of this file.

Thanks very much for your responses - very insightful

- Dan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic