• 李文文

    第 25 页:

    “如果线程A 因为调用了
    wait 系列函数、join 方法或者 sleep 方法而被阻塞挂起,这时候若线程B 调用线程
    A 的 interrupt() 方法,线程A 会在调用这些方法的地方抛出 InterruptedException 异
    常而返回。”

    我觉得 join 写在这里有误导,应该是当前执行 join 方法的环境线程(如果作者能提及 join 底层是 wait 实现,我想可能会更好),理由如下代码:

    public static void main(String[] args) {
            Thread threadOne = new Thread(() -> {
                for (;;) {
    
                }
            });
            threadOne.start();
            Thread mainThread = Thread.currentThread();
            Thread threadTwo = new Thread(() -> {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
               // 而不是 threadOne..interrupt();
                mainThread.interrupt();
            });
            threadTwo.start();
            try {
                // 主线程等待线程1执行完
                threadOne.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    李文文发表于 2019/1/14 20:44:31
    • 翟陆续

      这个确实有歧义,明白你的意思,应该修改为:如果线程A 因为调用了其他线程的
      join 方法而被阻塞挂起,这时候若线程B 调用线程
      A 的 interrupt() 方法,线程A 会在调用join方法的地方抛出 InterruptedException 异
      常而返回。

      翟陆续发表于 2019/1/31 18:51:41
  • SnowMan

    坐等PDF版.

    SnowMan发表于 2018/11/15 17:32:38
  • 1
  • 2
  • 3
  • 4
  • 5