Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing static methods from Threads  RSS feed

 
Chris Jebaraj
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends,

I have a basic doubt which made me to confuse a lot . I have an application which will support multi threading. From the thread i am accessing a static method in a class, (for eg: Utils.getCurrentDate() where getCurrentDate() is a static method in my Utils class). Now, when i have some five to ten threads running, will there be any synchronization problem when the threads are trying to access this static method. In other words, if one thread is accessing this Utils.getCurrentDate() method, then do other threads have to wait till the first thread finish off??? Please anyone clear me in this basic doubt. Would be more helpful.

Note: I just gave an example as Utils.getCurrentDate(), but in my actuall application, my static method is doing some major calculation which will take little time.

Thanks a lot.

 
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
If the method is not synchronized, then no threads will have to wait. If there are 4 processors, then four threads can be running the thread at the exact same time. If the method is synchronized, then of course only one thread can call it at a time.

If the method uses any changeable static data, then it needs to be synchronized. If it only uses local variables, then it probably doesn't.
 
Chris Jebaraj
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets take i have a singleton class, MyClass (so i will be maintaining a single instance in my application). What will happen if i access this singleton class's methods from the static method which i am accessing from my thread's run method. Also, none of the methods in the singleton class is synchronized. I hope no threads have to wait in this case too. Would you please correct me if i am wrong anywhere.
If the control goes deeper and deeper from my thread's run method, i am lossing my way, thats why confusion.

Thanks a lot.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeba Raj:
I hope no threads have to wait in this case too.


You're right, in general.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If the control goes deeper and deeper from my thread's run method, i am lossing my way, thats why confusion.


I cannot get the problem you are facing :-(.

./pope
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!