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 }
终止线程的典型方法!!!