SachinBeyond
V2EX  ›  问与答

问个技术问题, Java 中的队列中断的问题

  •  
  •   SachinBeyond · May 11, 2022 · 1471 views
    This topic created in 1645 days ago, the information mentioned may be changed or developed.
    如果保证队列中一直有数据,不会阻塞 take ,取出数据就不做任何处理,丢弃,然后取下一个。这个时候其他线程对他发起中断,那么这个消费线程会抛出中断异常吗?

    就是说 take 非阻塞状态为是否会抛出中断异常?
    2 replies  •  2022-05-11 16:54:46 +08:00
    Uyuhz
    Uyuhz
    May 11, 2022
    1
    不知道我有没有理解错问题...看了下 LinkedBlockingQueue 和 ArrayBlockingQueue 中的 take 方法(其他没看),无论是否有元素都会先调用 ReentrantLock 的 lockInterruptibly 方法(实际在 AQS 中),而这个方法在线程中断时就会抛出中断异常,根本不会走到 await 。
    SachinBeyond
    SachinBeyond
    OP
    May 11, 2022
    2
    @Uyuhz 好的谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1326 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 23:57 · PVG 07:57 · LAX 15:57 · JFK 18:57
    ♥ Do have faith in what you're doing.