I'm trying to write a code which wants to make a write thread. When I want to run it, I got this exception. Each post that I saw about this topic didn't have the code same as mine. So can any one help me about my problem?
java.lang.IllegalMonitorStateException
The stacktrace is as below:
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at prj.McWThread.ReadPacket(McWThread.java:40)
at prj.McWThread.run(McWThread.java:73)
The part of code that makes this exception is :
public void run()
{
try{
while (true)
{
this.MyPkt = ReadPacket();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(MyPkt);
}
}
}
Readpacket method:
public MyPacket ReadPacket()
{
MyPacket m = new MyPacket();
System.out.println("ReadPacket");
try {
while (Buff.isEmpty()) {
wait();
}
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
if (! Buff.isEmpty()) {
m = (MyPacket) Buff.remove(0);
return m;
} else {
return m;
}
}