kafka中的timertasklist.scala#add()方法死锁问题

beq87vna  于 2021-06-04  发布在  Kafka
关注(0)|答案(0)|浏览(162)

timertasklist#add()

def add(timerTaskEntry: TimerTaskEntry): Unit = {
    var done = false
    while (!done) {
      // Remove the timer task entry if it is already in any other list
      // We do this outside of the sync block below to avoid deadlocking.
      // We may retry until timerTaskEntry.list becomes null.
      timerTaskEntry.remove()

      synchronized {
        timerTaskEntry.synchronized {
          if (timerTaskEntry.list == null) {
            // put the timer task entry to the end of the list. (root.prev points to the tail entry)
            val tail = root.prev
            timerTaskEntry.next = root
            timerTaskEntry.prev = tail
            timerTaskEntry.list = this
            tail.next = timerTaskEntry
            root.prev = timerTaskEntry
            taskCounter.incrementAndGet()
            done = true
          }
        }
      }
    }
  }

timertasklist#删除()

// Remove the specified timer task entry from this list
      def remove(timerTaskEntry: TimerTaskEntry): Unit = {
        synchronized {
          timerTaskEntry.synchronized {
            if (timerTaskEntry.list eq this) {
              timerTaskEntry.next.prev = timerTaskEntry.prev
              timerTaskEntry.prev.next = timerTaskEntry.next
              timerTaskEntry.next = null
              timerTaskEntry.prev = null
              timerTaskEntry.list = null
              taskCounter.decrementAndGet()
            }
          }
        }
      }

timertaskentry#删除()

def remove(): Unit = {
            var currentList = list
            // If remove is called when another thread is moving the entry from a task entry list to another,
            // this may fail to remove the entry due to the change of value of list. Thus, we retry until the list becomes null.
            // In a rare case, this thread sees null and exits the loop, but the other thread insert the entry to another list later.
            while (currentList != null) {
              currentList.remove(this)
              currentList = list
            }
    }

以上是定时轮上铲斗操作的并发操作, timerTaskEntry.remove() 如果这条线移到 timerTaskEntry.synchronized 在内部,什么样的并发场景会死锁?
先看看计时轮的源码,新手很感激

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题