Background:
I have an application of miniature robots (these extend Thread
class) that make deals with one another at nodes of a map. I am trying to program the logic that goes into the node. The node is responsible for carrying
out the deals between two bots that meet at the node.
The logic I want to code into the node is as follows:
- Bot A arrives.
- IF there is another Bot present at the node (e.g. Bot B)
- then broker a deal between Bot A and Bot B.
- ELSE tell Bot A to wait until another Bot arrives at the Node.
My Attempt
Here is my attempt at coding the logic described above.
public void trade(StrippedBot trader)
{
// check to see if there are any other bots waiting
if(bots.size() > 0)
{
for (StrippedBot b : bots.keySet()) {
if(!b.equals(trader) && !b.getFamily().getName().equals(trader.getFamily().getName()))
{
b.notify();
trader.getResource().adjust(COOPERATION_REWARD);
b.getResource().adjust(COOPERATION_REWARD);
trace(trader);
}
}
} else {
// this is the first bot here, so shall wait for others to come.
try
{
bots.put(trader, true); // keeping track - true to mean this bot is on wait().
trader.wait(); // Tell Bot to wait till another bot comes along.
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
But when I run it, I get the IllegalMonitorStateException
on the trader.wait()
line. I have researched, and apparently it is a commonly encountered problem. So I tried the synchronized(trader)
but that just froze everything, I couldn't even debug through the code, as everything was frozen.