Java: waiting on synchronized block, who goes first?

后端 未结 4 1475
青春惊慌失措
青春惊慌失措 2020-12-06 10:30

This question is inspired by this other question.

If multiple threads are waiting on a synchronized block, and the lock becomes available, who goes firs

4条回答
  •  Happy的楠姐
    2020-12-06 11:14

    Someone else mentioned the availability of fair locks. If you really care who goes first, then you may have a real-time problem. In that case, you can make use of RTSJ, wherein the ordering and other semantics of lock acquisition is specified. The specifics are available in the RTSJ Spec under Synchronization. Quoting from the rationale section:

    Java's rules for synchronized code provide a means for mutual exclusion but do not prevent unbounded priority inversions and thus are insufficient for real-time applications. This specification strengthens the semantics for synchronized code by mandating priority inversion control, in particular by furnishing classes for priority inheritance and priority ceiling emulation. Priority inheritance is more widely implemented in real-time operating systems and thus is required and is the initial default mechanism in this specification.

提交回复
热议问题