| Author |
static method
|
Krishna Tota
Ranch Hand
Joined: Mar 22, 2008
Posts: 40
|
|
Hi, 1)Can we synchronize a static method?if yes what is the effect on multithreading? 2)say,we have class Abc{ static Myclass myobject=new Myclass(); } now,if we use synchronized static method to access the myobject using one thread. Can we use that myobject of that Myclass in another thread cuncarently: Dont forget the method in Sunchronized. please explain in brief. Thanks.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Originally posted by Krishna Tota: 2)say,we have class Abc{ static Myclass myobject=new Myclass(); } now,if we use synchronized static method to access the myobject using one thread. Can we use that myobject of that Myclass in another thread cuncarently:
myobject is an instance of Abc.so you can access this from different method concurrently. you are talking about myobject only right?
|
 |
Shikhar Madhok
Ranch Hand
Joined: Dec 18, 2006
Posts: 95
|
|
Originally posted by Krishna Tota: Hi, 1)Can we synchronize a static method?if yes what is the effect on multithreading? 2)say,we have class Abc{ static Myclass myobject=new Myclass(); } now,if we use synchronized static method to access the myobject using one thread. Can we use that myobject of that Myclass in another thread cuncarently: Dont forget the method in Sunchronized. please explain in brief. Thanks.
(My guess) If the only way to access myobject is through a method which is synchronized, then you will be able to access it only from one thread at a time. I think a static method can be synchronized as well. HTH Shikhar
|
He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever - Chinese proverb
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2552
|
|
Can we synchronize a static method?if yes what is the effect on multithreading?
Yes we can synchronize on a static method, the effect is mutual exclusion of threads.Similar to synchronizing on non static methods.
originally posted by seetharaman venkatasamy myobject is an instance of Abc.
No its not instance of Abc its instance of Myclass
myobject=new Myclass();
And in both cases it cannot be accessed concurrently by another thread. Hope this helps
|
SCJP, SCWCD.
|Asking Good Questions|
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Originally posted by Amit Ghorpade: And in both cases it cannot be accessed concurrently by another thread. Hope this helps
Thanks Amit but myObject(static) is an instance .so it is not synchronized right?so why can not access that concurrently? please ,can you explain me?
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2552
|
|
but myObject(static) is an instance .so it is not synchronized right?
Aren't instance accesses synchronized?
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Got it Amit ... Thanks
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2552
|
|
You are welcome
|
 |
 |
|
|
subject: static method
|
|
|