zoukankan      html  css  js  c++  java
  • 正确停止线程的方法

     1 package com.lhy.thread;
     2 
     3 public class ThreadStop {
     4 
     5     public static void main(String[] args) {
     6         Runner r  = new Runner();
     7         Thread t = new Thread(r);
     8         t.start();
     9         for(int i=0;i<100;i++){
    10             System.out.println("main thread i="+i);
    11         }
    12         System.out.println("main thread is over!");
    13         r.shutDown();
    14     }
    15 
    16 }
    17 
    18 class Runner implements Runnable {
    19     private boolean flag = true;
    20 
    21     public void run() {
    22 
    23         int i = 0;
    24         while (flag) {
    25             System.out.println("i= "+i);
    26             i++;
    27         }
    28     }
    29     public void shutDown(){
    30         flag = false;
    31         System.out.println("Runner thread is over!");
    32     }
    33 
    34 }
  • 相关阅读:
    nodejs获取服务器数据到页面
    Struts 2
    JQuery
    JDBC
    Hiberbate
    EasyUi
    JavaScript
    利用 HashSet 去过滤元素是否重复
    HTML
    MySQL
  • 原文地址:https://www.cnblogs.com/lihaoyang/p/4477622.html
Copyright © 2011-2022 走看看