• Post Reply Bookmark Topic Watch Topic
  • New Topic

The method in a singleton class  RSS feed

 
chaohua wang
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!