Are Thread.stop and friends ever safe in Java?

前端 未结 8 1130
清酒与你
清酒与你 2020-11-28 08:05

The stop(), suspend(), and resume() in java.lang.Thread are deprecated because they are unsafe. The Oracle recommended w

8条回答
  •  执笔经年
    2020-11-28 08:36

    All forms of concurrency control can be provided by the Java synchronization primitives by constructing more complex concurrency controls that suit your problem.

    The reasons for deprecation are clearly given in the link you provide. If you're willing to accept the reasons why, then feel free to use those features.

    However, if you choose to use those features, you also accept that support for those features could stop at any time.

    Edit: I'll reiterate the reason for deprecation as well as how to avoid them.

    Since the only danger is that objects that can be referenced by the stoped thread could be corrupted, simply clone the String before you pass it to the Thread. If no shared objects exist, the threat of corrupted objects in the program outside the stoped Thread is no longer there.

提交回复
热议问题