Java: how to abort a thread reading from System.in

前端 未结 6 2057
梦谈多话
梦谈多话 2020-12-01 14:35

I have a Java thread:

class MyThread extends Thread {
  @Override
  public void run() {
    BufferedReader stdin =
        new BufferedReader(new InputStream         


        
6条回答
  •  旧巷少年郎
    2020-12-01 15:31

    Have you tried:

    Thread myThread = new MyThread();
    myThread.start();
    // your code.
    myThread.interrupt();
    

    The interrupt method will throw an InterrupedtException and you can handle the code after that.

提交回复
热议问题