How to declare Thread Safe variable in Singleton Class?
Hi All,
I like to know how to declare Thread Safe variables in Singleton Class?
I.e....If different threads were acessing i don't want the variables values modified by some other thread. i want the default values and i will manipulate with the current thread.
Can somebody give me examples?
Thanks,
J.Kathir
[345 byte] By [
jkathira] at [2007-11-27 2:25:16]

How about using volatile?Will that meet your needs? Maybe you can give an example of how the variable is going to be used.Budyanto
Can't you use a method with default access priviliges to manipulate the variable. This means it can only be called from classes defined in the same package where the Singleton is defined.
> Hi All,
>
> I like to know how to declare Thread Safe variables
> in Singleton Class?
>
> I.e....If different threads were acessing i don't
> want the variables values modified by some other
> thread. i want the default values and i will
> manipulate with the current thread.
>
> Can somebody give me examples?
>
> Thanks,
> J.Kathir
you can use synchronized methods to block thread access in the singleton class.
For example add the attribute "boolean holded;" at the singleton and initalize it with a false value at singleton's initalization.
Then you can use ops:
public synchronized boolean hold()
// returning true if hold have been done, false other case
{
if (!holded) {holded=true; return true;}
else return false;
}
public void unhold()
// no mather if it's not synchronized because never 2 threads will use this at same time
{ holded=false;}
and when you need the singleton in a thread's run do :
public void run()
{
...
while (!singleton.getInstance().hold())
{... ops at singleton variables}
singleton.getInstance().unhold();
}
i wish it colld be usefull :)
I'm a bit confused by your question... If your threads share access to an object, but they cannot change it while another thread is running, then maybe you don't need it to be shared but exclusive to that thread. If you use synchronized access for the entire lifecycle of the thread, you might as well go without threads. How I understand it, you want some kind of prototype object that you duplicate for each thread instead of a singleton. (correct me if I'm wrong)
> > Hi All,
> >
> > I like to know how to declare Thread Safe
> variables
> > in Singleton Class?
> >
> > I.e....If different threads were acessing i don't
> > want the variables values modified by some other
> > thread. i want the default values and i will
> > manipulate with the current thread.
> >
> > Can somebody give me examples?
> >
> > Thanks,
> > J.Kathir
>
> you can use synchronized methods to block thread
> access in the singleton class.
> For example add the attribute "boolean holded;" at
> the singleton and initalize it with a false value at
> singleton's initalization.
> Then you can use ops:
> public synchronized boolean hold()
> // returning true if hold have been done, false other
> case
> {
>if (!holded) {holded=true; return true;}
> else return false;
> }
> public void unhold()
> // no mather if it's not synchronized because never 2
> threads will use this at same time
> { holded=false;}
>
> and when you need the singleton in a thread's run do
> :
> public void run()
> {
> ...
> while (!singleton.getInstance().hold())
> {... ops at singleton variables}
> singleton.getInstance().unhold();
> }
>
> i wish it colld be usefull :)
Isn't this how synchronizing on an object works?
> > > Hi All,
> > >
> > > I like to know how to declare Thread Safe
> > variables
> > > in Singleton Class?
> > >
> > > I.e....If different threads were acessing i
> don't
> > > want the variables values modified by some other
> > > thread. i want the default values and i will
> > > manipulate with the current thread.
> > >
> > > Can somebody give me examples?
> > >
> > > Thanks,
> > > J.Kathir
> >
> > you can use synchronized methods to block thread
> > access in the singleton class.
> > For example add the attribute "boolean holded;" at
> > the singleton and initalize it with a false value
> at
> > singleton's initalization.
> > Then you can use ops:
> > public synchronized boolean hold()
> > // returning true if hold have been done, false
> other
> > case
> > {
> >if (!holded) {holded=true; return true;}
> > else return false;
> > }
> > public void unhold()
> > // no mather if it's not synchronized because never
> 2
> > threads will use this at same time
> > { holded=false;}
> >
> > and when you need the singleton in a thread's run
> do
> > :
> > public void run()
> > {
> > ...
> > while (!singleton.getInstance().hold())
> > {... ops at singleton variables}
> > singleton.getInstance().unhold();
> > }
> >
> > i wish it colld be usefull :)
>
> Isn't this how synchronizing on an object works?
yes it's a very simple synchronuzation...
really and reading better the initial question (it was too soon before and my first answer was for synchronizing the singleton accesses), i don't think he need a singleton for this because he said " want the default values and i will manipulate with the current thread", so the best proposal to that could be not to use a singleton, but using a class wich creator initializes the default variables instead.
> really and reading better the initial question (it
> was too soon before and my first answer was for
> synchronizing the singleton accesses), i don't think
> he need a singleton for this because he said " want
> the default values and i will manipulate with the
> current thread", so the best proposal to that could
> be not to use a singleton, but using a class wich
> creator initializes the default variables instead.
Exactly my thoughts...
> i don't think
> he need a singleton for this because he said " want
> the default values and i will manipulate with the
> current thread", so the best proposal to that could
> be not to use a singleton, but using a class wich
> creator initializes the default variables instead.
If he needs to share a single resource among threads a Singleton is a good choise.
I interpret his question as if the Singleton has some internal state that can be set from the outside, but he now wants just one specific thread to be able to manipulate the Singleton state.
> I interpret his question as if the Singleton has some
> internal state that can be set from the outside, but
> he now wants just one specific thread to be able to
> manipulate the Singleton state.
if it's the case (i don't know) i think the best solution could be a class that can be instantiated for each thread and each class can access a singleton state machine representation wich can modify the state only by a single thread, in it's case the hold/unhold ops could be usefull.
Probably we need more information about the real problem and behaviour of what it's needed for this issue...