这本书中代码从哪找
页码:242 • 印次: 1
调用 awaitTermination 之后又调用了shutdown 方法
awaitTermination
shutdown
应该修改为:调用 awaitTermination 之前又调用了 shutdown 方法吧?
这个没问题的,这里说的是线程A调用了awaitTermination后会被阻塞,然后线程B调用了shutdown的情况
页码:228 • 印次: 1
一共有两处的:keeyAliveTime 笔误,应该修正为 keepAliveTime
keeyAliveTime
keepAliveTime
感谢指正,
第 134 页的生产-消费模型,生产者和消费者在 run 方法里面应该要有个 while(true) {} 包裹的吧:
Thread producer = new Thread(new Runnable() { @Override public void run() { // while(true) while(true) { // 获取独占锁 lock.lock(); try { // 如果队列满了,则等待 while (queue.size() == queueSize) { notEmpty.await(); } // 添加元素 queue.add("ele"); // 唤醒消费线程 notFull.signalAll(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } } });
如果作者能提及 BlockingQueue 的相应实现源码就更好了
BlockingQueue
嗯嗯,加上循环更好,本文的这个例子不够严谨
@翟陆续 您好,此示例确实无法体现出 condition 特性。
第 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(); } }
这个确实有歧义,明白你的意思,应该修改为:如果线程A 因为调用了其他线程的 join 方法而被阻塞挂起,这时候若线程B 调用线程 A 的 interrupt() 方法,线程A 会在调用join方法的地方抛出 InterruptedException 异 常而返回。
这本书中代码从哪找
页码:242 • 印次: 1
应该修改为:调用
awaitTermination
之前又调用了shutdown
方法吧?页码:228 • 印次: 1
第 134 页的生产-消费模型,生产者和消费者在 run 方法里面应该要有个 while(true) {} 包裹的吧:
如果作者能提及
BlockingQueue
的相应实现源码就更好了第 25 页:
我觉得 join 写在这里有误导,应该是当前执行 join 方法的环境线程(如果作者能提及 join 底层是 wait 实现,我想可能会更好),理由如下代码: