Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache kafka kafka中的TimerTaskList.scala#add()方法死锁问题 TimerTaskList#add() TimerTaskList#删除() TimerTaskEntry#删除()_Apache Kafka_Deadlock_Timertask - Fatal编程技术网

Apache kafka kafka中的TimerTaskList.scala#add()方法死锁问题 TimerTaskList#add() TimerTaskList#删除() TimerTaskEntry#删除()

Apache kafka kafka中的TimerTaskList.scala#add()方法死锁问题 TimerTaskList#add() TimerTaskList#删除() TimerTaskEntry#删除(),apache-kafka,deadlock,timertask,Apache Kafka,Deadlock,Timertask,以上是正时轮上铲斗操作的同时操作 timertasentry.remove()如果此行移动到timertasentry.synchronized内部,什么样的并发场景会死锁 首先看一下计时轮的源码,新手很感激 def add(timerTaskEntry: TimerTaskEntry): Unit = { var done = false while (!done) { // Remove the timer task entry if it is already

以上是正时轮上铲斗操作的同时操作

timertasentry.remove()
如果此行移动到
timertasentry.synchronized
内部,什么样的并发场景会死锁

首先看一下计时轮的源码,新手很感激

 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
          }
        }
      }
    }
  }

      // 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()
            }
          }
        }
      }

           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
            }
    }