各位大侠指点下.Interrupt()好像不能停止一个正处于IO阻塞中的线程,且无法生成InterruptException.那么一个线程如果因为IO操作处于阻塞状态时,该怎样来中止它呢?下面是我写的一段代码:class MM extends JFrame implements Runnable{......}addWindowListener(new WindowAdapter()//给MM添加监视器
{
public void windowClosing(WindowEvent we)
{
try
{
serverwork.interrupt(); //serverwork的定义:Thread serverwork=new Thread(MM)
System.exit(1);
}
catch(Exception e)
{}
}
});public void run() //MM中的Run方法
{
try
{
ss=new ServerSocket(4001); //ss是MM中的一个ServerSocket
Socket s=ss.accept();
while(true)
{
byte[] temp=new byte[100];
InputStream sis=s.getInputStream();
try
{
sis.read(temp); //进行IO操作时阻塞,WindowListener()中调用
//serverwork.interrupt()也无法终止它
jtareceive.append("Message from client : "+(new String(temp))+'\n'); }
catch(Exception e)//调用serverwork.interrupt()后捕捉不到interruptexcep.
{
sis.close();
s.close();
ss.close();
return;
}
}
}
catch(Exception e)
{}
}拜托各位了,谢谢!!!
{
public void windowClosing(WindowEvent we)
{
try
{
serverwork.interrupt(); //serverwork的定义:Thread serverwork=new Thread(MM)
System.exit(1);
}
catch(Exception e)
{}
}
});public void run() //MM中的Run方法
{
try
{
ss=new ServerSocket(4001); //ss是MM中的一个ServerSocket
Socket s=ss.accept();
while(true)
{
byte[] temp=new byte[100];
InputStream sis=s.getInputStream();
try
{
sis.read(temp); //进行IO操作时阻塞,WindowListener()中调用
//serverwork.interrupt()也无法终止它
jtareceive.append("Message from client : "+(new String(temp))+'\n'); }
catch(Exception e)//调用serverwork.interrupt()后捕捉不到interruptexcep.
{
sis.close();
s.close();
ss.close();
return;
}
}
}
catch(Exception e)
{}
}拜托各位了,谢谢!!!
If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException照这段话,调用被阻塞thread.interrupt()应该还是能够起作用,并会抛出interrupt异常啊?