P
Pep
I need to let a number of threaded objects change the state of a singular
object by use of setter methods.
What would be the better approach to synchronise based on a couple of
boolean variables. Should I use a couple of synchronised method members or
make the boolean variables synchronised?
class x
// using synchronised methods
{
boolean A = false;
boolean B = false;
public synchronized void setA()
{
a = true;
}
public synchronized void unsetA()
{
a = false;
}
private synchronized void basedOnAandB()
{
if ((a) && (!b))
{
// do something
}
}
}
class x
// using synchronised variables
{
SynchronizedBoolean A = new SynchronizedBoolean(false);
SynchronizedBoolean B = new SynchronizedBoolean(false);
public void setA()
{
A.set(true);
}
public void unsetA()
{
A.set(false);
}
private void basedOnAandB()
{
if (A.equals(B.equals(false)))
{
// do something
}
}
}
Note, I am assuming that because the B.equals method is called from the
parameter list of the A.equals method that this is as close as I can get to
a logical "and" operation that has the B variable locked for the duration
of the "A" method call.
TIA,
Pep.
object by use of setter methods.
What would be the better approach to synchronise based on a couple of
boolean variables. Should I use a couple of synchronised method members or
make the boolean variables synchronised?
class x
// using synchronised methods
{
boolean A = false;
boolean B = false;
public synchronized void setA()
{
a = true;
}
public synchronized void unsetA()
{
a = false;
}
private synchronized void basedOnAandB()
{
if ((a) && (!b))
{
// do something
}
}
}
class x
// using synchronised variables
{
SynchronizedBoolean A = new SynchronizedBoolean(false);
SynchronizedBoolean B = new SynchronizedBoolean(false);
public void setA()
{
A.set(true);
}
public void unsetA()
{
A.set(false);
}
private void basedOnAandB()
{
if (A.equals(B.equals(false)))
{
// do something
}
}
}
Note, I am assuming that because the B.equals method is called from the
parameter list of the A.equals method that this is as close as I can get to
a logical "and" operation that has the B variable locked for the duration
of the "A" method call.
TIA,
Pep.