线程对象.interrupt()

注意,异常分析中要有break,否则无法中断

public class Demo extends JFrame {
private Thread thread;//定义线程
final JProgressBar progressBar = new JProgressBar();//进度条 public Demo() {
setBounds(100, 100, 200, 100);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(progressBar, BorderLayout.NORTH);
progressBar.setStringPainted(true);//显示数字
//使用匿名内部类实现线程对象
thread = new Thread() {
int count = 0;//进度数据 public void run() {
while (true) {//无限循环,一般在不知道循环次数时使用
progressBar.setValue(++count);
if (count == 20) {
//thread.interrupt();//如果thread=new Thread(new Runnalbe{...}),this无效
this.interrupt();//this指代Thread()
}
try {
Thread.sleep(100);//休眠0.1s
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("20%处被中断");//抛出异常
break;//一定要有break,否则无法中断
}
}
}
};
thread.start(); setVisible(true);
} public static void main(String[] args) {
new Demo();
}
}

最新文章

  1. RPC hessian简单案例
  2. bzoj1104: [POI2007]洪水pow
  3. RMI之HelloWorld尝试
  4. 警惕SQL语句陷井
  5. java.lang.NoClassDefFoundError: org/objectweb/asm/Type
  6. android 应用架构随笔四(View、ViewGroup)
  7. 初步探讨WPF的ListView控件(涉及模板、查找子控件)
  8. 创建MySQL用户 赋予某指定库表的权限 flush privileges才能生效!!!!;@'localhost'授权本地,@'%'授权远程
  9. ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解
  10. QQ群聊天记录文件分割
  11. java小知识点汇总
  12. HBuilder git合作-从Git Hub Clone项目
  13. 【原】Java学习笔记015 - 面向对象
  14. Sublime Text3使用Package Control 报错There Are No Packages Available For Installation
  15. Peaceful Commission HDU - 1814(输出最小的一组解)
  16. Flume:sink.type=hive
  17. 【模态窗口-Modeldialog】提交请求时禁止在新窗口打开页面的处理方法
  18. hihocoder第196周
  19. 第四周PSP&进度条
  20. bzoj4998: 星球联盟(link-cut-tree)

热门文章

  1. 牛客国庆集训派对Day6 B.Board
  2. oracle alter
  3. myBatis插件(plugins)
  4. JWT验证
  5. vue 项目引入字体报错
  6. vue element-ui upload 实现带token上传
  7. postman发送json格式的post请求
  8. js面向对象高级编程
  9. [转帖] securebootthemes 挖矿病毒的说明 http://blog.netlab.360.com/msraminer-qian-fu-yi-jiu-de-wa-kuang-jiang-shi-wang-luo/ 原文为毛不给一个专杀工具呢.
  10. Laravel之路由 Route::get/post/any、路由参数、过滤器、命名、子域名、前缀、与模型绑定、抛出 404 错误、控制器