线程的中断.interrupt
2024-10-19 04:31:26
线程对象.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();
}
}
最新文章
- RPC hessian简单案例
- bzoj1104: [POI2007]洪水pow
- RMI之HelloWorld尝试
- 警惕SQL语句陷井
- java.lang.NoClassDefFoundError: org/objectweb/asm/Type
- android 应用架构随笔四(View、ViewGroup)
- 初步探讨WPF的ListView控件(涉及模板、查找子控件)
- 创建MySQL用户 赋予某指定库表的权限 flush privileges才能生效!!!!;@'localhost'授权本地,@'%'授权远程
- ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解
- QQ群聊天记录文件分割
- java小知识点汇总
- HBuilder git合作-从Git Hub Clone项目
- 【原】Java学习笔记015 - 面向对象
- Sublime Text3使用Package Control 报错There Are No Packages Available For Installation
- Peaceful Commission HDU - 1814(输出最小的一组解)
- Flume:sink.type=hive
- 【模态窗口-Modeldialog】提交请求时禁止在新窗口打开页面的处理方法
- hihocoder第196周
- 第四周PSP&;进度条
- bzoj4998: 星球联盟(link-cut-tree)
热门文章
- 牛客国庆集训派对Day6 B.Board
- oracle alter
- myBatis插件(plugins)
- JWT验证
- vue 项目引入字体报错
- vue element-ui upload 实现带token上传
- postman发送json格式的post请求
- js面向对象高级编程
- [转帖] securebootthemes 挖矿病毒的说明 http://blog.netlab.360.com/msraminer-qian-fu-yi-jiu-de-wa-kuang-jiang-shi-wang-luo/ 原文为毛不给一个专杀工具呢.
- Laravel之路由 Route::get/post/any、路由参数、过滤器、命名、子域名、前缀、与模型绑定、抛出 404 错误、控制器