 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
pershing Guest
|
Posted: Thu Dec 28, 2006 8:11 am Post subject: Synchronization question |
|
|
Let say I have a class named Home.
public class Home{
public Home() {}
public int getColor() {...)
public int getSize() {...}
public setColor(...) {...}
public setSize(...) {...}
}
(The actual code is irrelevant)
If a method is called from that class by a thread, I would like to cut
the possibility of another thread calling this method or any other
methods of the same instance of this class until the execution of that
method is completed.
An example:
Given a single instance of Home as home.
Thread 1 calls home.getSize();
Thread 2 calls home.getColor(); but since Thread1 is already calling a
method of class home (same instance), it is awaiting until Thread1
finishes using home.
Thread 1 finishes with home.getSize();
Thread 2 can now enter home.getColor();
| Quote: | From what I understand putting synchronized into
|
public synchronized int GetColor() {...}
would only work on that method but any other method in home could be
called even if GetColor is already being used.
Is there a simple way to allow only one access to any method of an
instance of a class at the same time?
Thanks for any input. |
|
| Back to top |
|
 |
Gordon Beaton Guest
|
Posted: Thu Dec 28, 2006 8:11 am Post subject: Re: Synchronization question |
|
|
On 27 Dec 2006 22:19:45 -0800, pershing wrote:
| Quote: | From what I understand putting synchronized into
public synchronized int GetColor() {...}
would only work on that method but any other method in home could be
called even if GetColor is already being used.
Is there a simple way to allow only one access to any method of an
instance of a class at the same time?
|
Declare *all* of them synchronized.
/gordon
--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e |
|
| Back to top |
|
 |
pershing Guest
|
Posted: Sat Dec 30, 2006 8:11 am Post subject: Re: Synchronization question |
|
|
Thanks to you all!
Sundar wrote:
| Quote: | The following tutorial will be helpful in understanding
synchronization.
http://java.sun.com/docs/books/tutorial/essential/concurrency/syncmeth.html
-Sundar
On Dec 28, 11:19 am, "pershing" <pershin...@gmail.com> wrote:
Let say I have a class named Home.
public class Home{
public Home() {}
public int getColor() {...)
public int getSize() {...}
public setColor(...) {...}
public setSize(...) {...}
}(The actual code is irrelevant)
If a method is called from that class by a thread, I would like to cut
the possibility of another thread calling this method or any other
methods of the same instance of this class until the execution of that
method is completed.
An example:
Given a single instance of Home as home.
Thread 1 calls home.getSize();
Thread 2 calls home.getColor(); but since Thread1 is already calling a
method of class home (same instance), it is awaiting until Thread1
finishes using home.
Thread 1 finishes with home.getSize();
Thread 2 can now enter home.getColor();
From what I understand putting synchronized intopublic synchronized int GetColor() {...}
would only work on that method but any other method in home could be
called even if GetColor is already being used.
Is there a simple way to allow only one access to any method of an
instance of a class at the same time?
Thanks for any input. |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|