How to access the running thread/runnable?

后端 未结 5 860
我寻月下人不归
我寻月下人不归 2020-12-24 04:10

I have a thread running but from outside I can\'t bypass a value to stop that thread. How can I send false/true value inside Mytest() or call running thread pub

5条回答
  •  醉梦人生
    2020-12-24 04:45

    Apart from the fact, that this thread is a heating test for your CPU ;)

    You can call the start/stop methods with

     MyThread.start();
     MyThread.stop();
    

    You've defined them as static methods, so the above lines of code show how to call them.

    for the heating... add something like

    try {
        Thread.sleep(100);  // value is milliseconds
    } catch (InterruptedException e) {
        // no need to handle (in this example)
    }
    

    This will reduce the CPU load from 100% (on one core) to a reasonable value ;)

提交回复
热议问题