有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    当然。只需从另一个线程调用theThreadThatJoined.interrupt()

    public class JoinInterruptedExceptionExample {
    public static void main(String[] args) {
       final Thread workerThread = new Thread(new Runnable() {
         @Override
         public void run() {
            try {
               Thread.sleep(20000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
         }
       });
       workerThread.start();
    
       final Thread waitingThread = new Thread(new Runnable() {
         @Override
         public void run() {
            try {
               workerThread.join();
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
         }
       });
       waitingThread.start();
    
       waitingThread.interrupt();
    
    }
    }