File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static method

 
Krishna Tota
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but myObject(static) is an instance .so it is not synchronized right?


Aren't instance accesses synchronized?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it Amit ... Thanks
 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic