Is it possible to create a high-priority thread with a regular JVM, one that wil not be preempted by any other thread?

前端 未结 3 1331
醉话见心
醉话见心 2021-01-14 11:07

So my goal is simple: I want to create a high-priority thread in Java without using RTSJ (Real-Time Java VM) or some other proprietary JVM. Let\'s assume you never create an

3条回答
  •  有刺的猬
    2021-01-14 11:45

    No. Standard Java doesn't support this.

    I want as much real-time response with as little latency as possible without having to go to RTSJ and special OSs

    "As little latency as possible" is not a quantifiable requirement.

    However, as I and others have said in response to your many questions in this area, JavaSE is not designed for this kind of thing, and doesn't / cannot provide any guarantees of latency. Indeed, many aspects of the generic Java core libraries are specified to make it clear that there are no performance-related guarantees on the standard Java platforms.

    In respect to this particular question, the Java threading specs say that thread priorities are advisory only. There are no guarantees that a high priority thread won't be preempted by a lower priority thread. Actual thread scheduling and switching policies are implemented by the host operating system and are beyond Java's control.

    Let's assume that it is possible to hack and/or use a linux distribution tuned not to do that.

    If you are making that assumption, you may as well just hack the operating system to make the thread scheduler, etc behave how you want ... assuming that that it is technically feasible. But also beware that hackery at that level can result in unanticipated problems for applications (like the JVM) that are not designed to cope with (for example) non-preemptable threads preventing the GC from running.

    If I at least can detect preemption I will have a way to measure my progress.

    A Java thread can't detect that it has been preempted, or that another Java thread has been preempted. I don't think that the OS-level native thread APIs that the JVM uses support this.

提交回复
热议问题