编程知识 cdmana.com

Control thread termination

public class Terminate implements Runnable{
    public boolean flag=true;
    public String name;

    public Terminate(String name) {
        this.name = name;
    }
    int i=0;
    @Override
    public void run() {
        while(flag){
            System.out.println(name+"-->"+i++);
        }

    }
// External method change logo 
    void terminate(){
       this.flag=false;


    }

    public static void main(String[] args) {
        Terminate t=new Terminate("c ROM. ");
        new Thread(t).start();


        for (int i = 0; i < 100; i++) {
            if(i==80){
                t.terminate();// Control thread termination // The key step 
                System.out.println("c ROM. gama over");

            }
            System.out.println("main-->"+i);

        }
    }
}

版权声明
本文为[8Years]所创,转载请带上原文链接,感谢

Scroll to Top