• 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

The method in a singleton class

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

There is a public calculate() method a singleton class.
If there are 2 threads to call the calculate() at the same time. I wonder if one thread must wait until another thread executing calculate() is finished?

If it is not a singleton class. I changed the mthod to static public calculate().
I still wonder if one thread must wait until another thread executing calculate() is finished?

Thank you!

Chaohua
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether or not you have a singleton, nothing automatically waits without some synchronization code.
And don't change a method that should not static to be static!
[ January 03, 2006: Message edited by: Jeff Albrechtsen ]
 
Ranch Hand
Posts: 686
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you don't put volatile key word for the variable, thread will have a their own local copy even though variable is static when threads are not synchronized.
[ January 03, 2006: Message edited by: Jignesh Patel ]
 
chaohua wang
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, guys,

I am still confused about static method.
I know a static varialbe has only one in memory.
I think static method also has only one in memory, can not make a copy for the threads. So 2 threads can only call it one by one until one calling is done.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jignesh Patel:
if you don't put volatile key word for the variable, thread will have a their own local copy even though variable is static when threads are not synchronized.





No. Some writes to a single non-volatile variable may be hidden from other threads for some period of time. You absolutely cannot look upon this as a feature you depend on for program correctness!
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chaohua wang:

I think static method also has only one in memory, can not make a copy for the threads.



In general, there is only ever a single copy of the code for any method, static or not (barring complications like multiple class loaders, which we'll ignore for the moment.) But this does not stop multiple threads from using that code at the same time, any more than you and a friend have any problem both looking out a single window at the same time.

Only the "synchronized" keyword can mark a method so that only one thread can use it at a time.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Extending on what Ernest said, every method call gets its own stack space for local variables and the like - whether they are in the same thread (recursion) or not.
 
reply
    Bookmark Topic Watch Topic
  • New Topic