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 }