I have a thread that is started when my web application starts (contextInitialized). All this thread does is, read data from the socket.
public void run() {
while (!Thread.currentThread().isInterrupted() || !this.isInterrupted()
|| !stopped) {
try {
System.out.println("************polling..............");
readData();
Thread.sleep(300);
} catch (InterruptedException e) {
System.out.println("interrupted... ");
Thread.currentThread().interrupt();
break; //required?
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void shutdown() {
stopped = true;
}
// I know I'm blocking here.
private void readData() {
if (null == socket)
throw new ConnectionException();
if (inStream == null) {
inStream = this.socket.getInputStream();
}
byte[] buff = new byte[length];
int receiveLength = 0;
do {
try {
receiveLength = inStream.read(buff, 0, length);
} catch (Exception e) {
e.printStackTrace();
}
} while (receiveLength <= 0);
}
I call interrupt() method in the contextDestroyed, but this thread fails to stop, why is that?
if (poller != null) {
((Poller) poller).shutdown();
poller.interrupt();
// should I need to do this?
while (poller.isAlive()) {
;
}
}