Hello, I have started working with threads in my curent project and I am having a lot of trouble understanding them. From what I understand if I have two threads (call them A and B) then thread A can call wait on thread B and thread A will pause until thread B calls notify() or notifyAll().
I am implementing a simple message handler that reads messages from a socket and puts them into a LinkedList. This code is in the run() method of another thread. From my main program I start up that thread running and then I call a function to get a message from it. Inside that function, a wait() is called if there is currently nothing in the LinkedList. When something is put into the LinkedList, the reader method calls notifyAll() and I get my "IllegalMonitorException: current thread not owner" error.
Here is the code:
Code:
public class DatagramMessageInterface extends java.lang.Thread implements PortListener
{
	private java.net.DatagramSocket dgsSocket;
	private int port;

	private java.util.LinkedList messageQueue = new java.util.LinkedList();

	private boolean listen;

	public DatagramMessageInterface() {
		super("DatagramMessageListenerThread");
		this.port = 4040;
	}
	public DatagramMessageInterface(int port) {
		super("DatagramMessageListenerThread");
		this.port = port;
	}

	public void initialize()	{
		try {
			this.dgsSocket = new java.net.DatagramSocket(this.port);
		} catch ( java.net.SocketException e ) {
			e.printStackTrace();
		}
	}

	public void run()	{
		byte[] buf = new byte[2048];
		java.net.DatagramPacket packet = new java.net.DatagramPacket(buf, buf.length);
		try {
			while( this.listen )
			{
				this.dgsSocket.receive(packet);
				java.net.InetAddress addr = packet.getAddress();
				this.messageQueue.addLast(MessageParser.UDP.parseMessage(new String(packet.getData()).substring(0,packet.getLength())));
				((NetMessage)this.messageQueue.getLast()).setToAddress(this.dgsSocket.getLocalAddress(), this.port);
				this.notifyAll();
			}
		} catch ( java.io.IOException e ) {
			e.printStackTrace();
		}
	}

	public int getQueueSize() {
		return this.messageQueue.size();
	}
	public NetMessage getMessage() {
		synchronized(this) {
			try {
				while( this.messageQueue.size() == 0 )
					this.wait();
			} catch ( InterruptedException e ) {
				e.printStackTrace();
			}
			return (NetMessage)this.messageQueue.removeFirst();
		}
	}
	public NetMessage getMessage(int maxWait) {
		if( this.messageQueue.size() > 0 )
			return (NetMessage)this.messageQueue.removeFirst();
		synchronized(this) {
			try {
				this.wait(maxWait);
			} catch ( InterruptedException e ) {
				e.printStackTrace();
			}
			if( this.messageQueue.size() > 0 )
				return (NetMessage)this.messageQueue.removeFirst();
			else
				return null;
		}
	}

	public void setStatus(boolean listen) {
		this.listen = listen;
	}
	public boolean isListening() {
		return this.listen;
	}
	public void write(NetMessage nm, java.net.InetAddress adr, int port) throws java.io.IOException {
		System.out.println("Address: "+adr+" Port:"+port);
		byte[] buffer = nm.toUDPBufferContent().getBytes();
		java.net.DatagramPacket dgp = new java.net.DatagramPacket(buffer, buffer.length, adr, port);
		System.out.println(nm);
		this.dgsSocket.send(dgp);
	}
}
Does anyone have any ideas as to what is happening? Is there something that I am missing about how threads, wait() and notifyAll() work? Thanks a lot.