I have a class that looks like the following
Note the user create a new NetworkingThread and then invokes the enableListener and disableListener as they wish.
Problem is after the disableListener is first called, the first message that arrived afterwards is processed. Should the following code be sufficient. Isn't the "run" and the "disableListener" meant to be running on the same thread anyway?
public class NetworkingThread implements Runnable
{
Thread listener;
Boolean listen;
public NetworkingThread()
{
listen = new Boolean(false);
listener = new Thread(this);
listener.start()
}
public void enableListener()
{
synchronized(listen)
{
listen = new Boolean(true);
}
}
public void disableListener()
{
synchronized(listen)
{
listen = new Boolean(false);
}
}
public void run()
{
synchronized(listen)
{
while (true)
{
while(listen.booleanValue())
{
//listen and process messages
}
}
}
}
[/code]
Code:
public class NetworkingThread implements Runnable
{
Thread listener;
Boolean listen;
public NetworkingThread()
{
listen = new Boolean(false);
listener = new Thread(this);
listener.start()
}
public void enableListener()
{
listen = new Boolean(true);
}
public void disableListener()
{
listen = new Boolean(false);
}
public void run()
{
while (true)
{
while(listen.booleanValue())
{
//listen and process messages
}
}
}
Note the user create a new NetworkingThread and then invokes the enableListener and disableListener as they wish.
Problem is after the disableListener is first called, the first message that arrived afterwards is processed. Should the following code be sufficient. Isn't the "run" and the "disableListener" meant to be running on the same thread anyway?
public class NetworkingThread implements Runnable
{
Thread listener;
Boolean listen;
public NetworkingThread()
{
listen = new Boolean(false);
listener = new Thread(this);
listener.start()
}
public void enableListener()
{
synchronized(listen)
{
listen = new Boolean(true);
}
}
public void disableListener()
{
synchronized(listen)
{
listen = new Boolean(false);
}
}
public void run()
{
synchronized(listen)
{
while (true)
{
while(listen.booleanValue())
{
//listen and process messages
}
}
}
}
[/code]