用线程终止while循环

    技术2022-05-20  69

    public class StartTest { public static void main(String[] args) { Server s = new Server(); s.start(); s.end(); } } public class Server { private Thread thread; public void start(){ MyThread myThread = new MyThread(); thread = new Thread(myThread); thread.setDaemon(true); thread.start(); } public void end(){ thread.interrupt(); } private class MyThread implements Runnable{ public void run(){ int i = 0; while(true){ System.out.println(i++); } } } }


    最新回复(0)