AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Synchronization question

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
pershing
Guest





PostPosted: Thu Dec 28, 2006 8:11 am    Post subject: Synchronization question Reply with quote



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





PostPosted: Thu Dec 28, 2006 8:11 am    Post subject: Re: Synchronization question Reply with quote



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





PostPosted: Sat Dec 30, 2006 8:11 am    Post subject: Re: Synchronization question Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.