结束线程的方法

当线程的run方法结束,线程就结束了,结束一个正在运行的线程,怎么实现呢,示例如下

public class TestThread4 {
    public static void main(String args[])

   {

        Runner4 r = new Runner4();
        Thread t = new Thread(r);
        t.start();
        for(int i=0;i<100000;i++){
            if(i%10000==0 & i>0)
            System.out.println("in thread main i=" + i);
        }
        System.out.println("Thread main is over");
        r.shutDown();
        //t.stop();
    }
}

class Runner4 implements Runnable {
    private boolean flag=true;
    public void run() {
        int i = 0;
        while (flag==true) {
            System.out.print(" " + i++);
        }
    }

    public void shutDown() {
        flag = false;
    }
}

原文地址:https://www.cnblogs.com/zhangkefan/p/4770666.html