Mutithreading with System.out.format and System.out.println

前端 未结 4 1411
梦毁少年i
梦毁少年i 2021-01-14 16:14

I came across this example on Oracle\'s Java Tutorial describing Deadlock in multi threading scenarios.

So in this example I made following change at line 17 and lin

4条回答
  •  遥遥无期
    2021-01-14 16:42

    You are mixing up things here.

    The fact that a piece of code can lead into a deadlock situation doesn't necessarily mean that you receive a deadlock each and any time that code runs.

    That is one of the aspects that makes multi-threading such a hard topic: if you run your code once, or 10 times, or a 100 times, and everything "works"; it is still possible that it will fail the next time.

    In other words: try putting that code in a loop on the outermost level, and sooner or later (probably sooner; if you don't do much "sleeping") you should hit the deadlock!

    If things were that easy and deadlocks could be detected that easily, we wouldn't need all those books and libraries and ideas how to deal with multi-threading ...

提交回复
热议问题