线程中断

huyanqieyu 2008-03-23 09:16:16
import java.awt.Frame;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ww
{
public static void main(String[] args)
{
int i;
final Mythread m=new Mythread();
Frame f=new Frame();
f.setSize(555, 555);

f.setLayout(new GridLayout(13,5));
Button b=new Button("按钮");
Button c=new Button("按钮c");

f.add(b);
f.add(c);
f.setVisible(true);

f.addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{
m.start();
System.out.print(" end");

}
});
}
}



class Mythread extends Thread
{
public void run()
{
for(int i=0;i<12;i++)
{
try
{
Thread.sleep(332);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if(i==4)
this.interrupt();

System.out.print(i);
}
}
}


运行结果怎么会这样;
end012345java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at Mythread.run(ww.java:52)
67891011


当i=4的时候线程不是应该中断吗,
...全文
84 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
eimhee 2008-03-23
  • 打赏
  • 举报
回复
当I=4时, 是中断了, 由于I==4时, 那线程处于SLEEP状态, 然后又调用了INTERRUPT()方法, 就会抛出InterruptedException,
在I=5时, e.printStackTrace(); 会退出程序,


线程进行中断就是抛出异常

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧