官术网_书友最值得收藏!

Deadlocks

Often, in order to guarantee that concurrent code is synchronized correctly, it's necessary to suspend or block execution while a task is completed in a different thread. But due to the complexity of these situations, it isn't uncommon to end up in a situation where the execution of the complete application is halted because of circular dependencies:

lateinit var jobA : Job
lateinit var jobB : Job

fun main(args: Array<String>) = runBlocking {
jobA = launch {
delay(1000)
// wait for JobB to finish
jobB.join()
}

jobB = launch {
// wait for JobA to finish
jobA.join()
}

// wait for JobA to finish
jobA.join()
println("Finished")
}

Let's take a look at a simple flow diagram of jobA.

Flow chart of jobA

In this example, jobA is waiting for jobB to finish its execution; meanwhile jobB is waiting for jobA to finish. Since both are waiting for each other, none of them is ever going to end; hence the message Finished will never be printed:

Flow chart of jobB

This example is, of course, intended to be as simple as possible, but in real-life scenarios deadlocks are more difficult to spot and correct. They are commonly caused by intricate networks of locks, and often happen hand-in-hand with race conditions. For example, a race condition can create an unexpected state in which the deadlock can happen.

主站蜘蛛池模板: 铜梁县| 蓬莱市| 黄骅市| 同江市| 昭苏县| 苏尼特右旗| 图木舒克市| 旬邑县| 清河县| 景德镇市| 化隆| 蕲春县| 龙井市| 榆社县| 独山县| 车险| 乐平市| 来凤县| 蒙阴县| 丘北县| 遵义市| 苍溪县| 丰都县| 渝中区| 山丹县| 讷河市| 上思县| 竹山县| 武安市| 汉中市| 大化| 松滋市| 措美县| 常宁市| 长泰县| 磴口县| 和硕县| 保德县| 鄯善县| 万山特区| 蛟河市|