teststop.java

来自「java2 primer plus一书源程序」· Java 代码 · 共 72 行

JAVA
72
字号
/* * TestStop.java * * Created on September 25, 2002, 12:08 PM */package ch18;/** * * @author  Stephen Potts */public class TestStop extends Thread{    private volatile boolean stopping;    private Thread secondThread;        /** Creates a new instance of TestStop */    public TestStop()    {    }        public void run()    {        secondThread = Thread.currentThread();        stopping = false;                int counter = 0;                while( !stopping)        {            System.out.println("counter = " + counter);            counter++;                        try            {                Thread.sleep(1000);            }catch (InterruptedException ie)            {                System.out.println("Sleep interrupted in run()");            }                    }    }        public void stopIt()    {        this.stopping = true;    }        public static void main(String[] args)    {        TestStop ts = new TestStop();        Thread t = new Thread(ts);        t.start();                //Delay for a few seconds to let the other thread get going        try        {            Thread.sleep(2500);        }catch (InterruptedException ie)        {            System.out.println("Sleep interrupted in main()");        }                System.out.println("About to stop the other thread");        ts.stopIt();        System.out.println("Exiting from Main");            }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?