首页 > 编程语言 > 详细

外部方式终止线程运行(标志位)

时间:2020-07-07 22:32:00      阅读:64      评论:0      收藏:0      [点我收藏+]
 1 /**
 2  *  线程外部终止(提供标志位用于终止线程)
 3  */
 4 public class ThreadStop implements Runnable{
 5     private boolean flag = true;
 6     private int a = 0;
 7     
 8     @Override
 9     public void run() {
10         while(flag) {
11             System.out.println("正在运行:" + a++);
12         }
13     }
14     
15     public void checkThread() {    //提供给外部的终止方法
16         this.flag = false;
17     }
18     
19     public static void main(String[] args) {
20         ThreadStop threadStop = new ThreadStop();
21         new Thread(threadStop).start();
22         for(int a = 0;a<99;a++) {
23             System.out.println("main"+a);
24             if(a == 50) {    //线程终止条件
25                 System.out.println("main"+a);
26                 threadStop.checkThread();
27                 System.out.println("线程终止");
28             }
29         }
30     }
31 }

 

外部方式终止线程运行(标志位)

原文:https://www.cnblogs.com/zhang741741/p/13263523.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!