Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

locking style

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static synchronized Data getInstance?
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic