线程的停止

复制代码
 1 package com.cn.threadstop;
 2 
 3 public class StopThread implements Runnable {
 4     String name;
 5     boolean live = true;
 6 
 7     public StopThread(String name) {
 8         super();
 9         this.name = name;
10     }
11 
12     @Override
13     public void run() {
14         int i=0;
15         while(live){
16             System.out.println(name+(i++));
17         }
18     }
19     
20     public void terminate(){
21         live=false;
22     }
23 
24     public static void main(String[] args) {
25         StopThread st = new StopThread("线程A");
26         Thread t1 = new Thread(st);
27         t1.start();
28         for(int i=0; i<1000;i++){
29             System.out.println(i);
30         }
31         st.terminate();
32         System.out.println("st stop");
33     }
34 }
复制代码

终止线程的典型方法!!!

posted @ 2013-07-27 23:44  沽名钓誉  阅读(183)  评论(0)    收藏  举报
编辑推荐:
· 微服务架构学习与思考:SOA架构与微服务架构对比分析
· tomcat为什么假死了
· 聊一聊 Linux 上对函数进行 hook 的两种方式
· C# 锁机制全景与高效实践:从 Monitor 到 .NET 9 全新 Lock
· 一则复杂 SQL 改写后有感
阅读排行:
· 完成微博外链备案,微博中直接可以打开园子的链接
· 推荐 3 种 .NET Windows 桌面应用程序自动更新解决方案
· .NET 10 支持Linux/Unix 的Shebang(Hashbang)
· 一个基于 .NET 开源、模块化 AI 图像生成 Web 用户界面
· 上周热点回顾(6.9-6.15)
点击右上角即可分享
微信分享提示