Win a copy of Murach's MySQL this week in the JDBC and Relational Databases 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread synchronization for Static factory method

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am trying to understand if I should synchronize the below factory method.I tested the code with three sample thread and I am always getting the correct object for each thread(meaning Thread 1 always shows TYPE1,thread2 always show TYPE2 and thread3 always shows TYPE3 but I am still thinking that this may not be the case always unless I synchronize the factory method else it will not be thread safe meaning thread 1 could get type 2 or type 3 also.

Also another option is moving the static field b inside method so that it will be a local variable.

Please correct me If am wrong.

Thanks in advance.



Factory Class:

 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to reproduce the error, you will need more than a 100 iterations in the loop. It could be that 100 loops run so fast that the first thread is done before the second starts

Making b into a local variable is a much better option. Each thread will get it's own variable.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your input but I was able to prove that this is not threadsafe (atleast in this case) by adding Thread.sleep(200) in the Facotry method and I see that thread started getting wrong object.
 
Master Rancher
Posts: 4757
71
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent. But Jayesh's other point stands. Your class field b is completely unnecessary and should be removed; it's the source of the bug you're observing. I would also recommend making the binders variable final. That's not strictly necessary for a static field, but good practice (and prevents anyone else from changing it without realizing it's supposed to be constant). If the field were non-static, then making it final would be necessary in order to access it in a thread-safe manner without synchronization.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic