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
I believe you're looking for this: http://en.wikipedia.org/wiki/Real_time_Java Regular Java doesn't provide such an ability.
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.
Yes it is possible! But isolating a cpu core so it is not preempted by any user or kernel process is a OS configuration unrelated to your application. After you do that you can use a thread affinity library such as CoralThreads to pin your thread to the isolated cpu core. This greatly reduces variance. Below a simple code example so you get the idea:
import com.coralblocks.coralthreads.Affinity;
public class Basics {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// must be the first thing inside the run method
Affinity.bind();
try {
while(true) {
// do whatever you want here...
}
} finally {
// must be the last thing inside the run method
Affinity.unbind();
}
}
}, "MyPinnedThread");
System.out.println();
Affinity.printSituation(); // nothing done yet...
// assign thread to processor:
int procToBind = Integer.parseInt(args[0]);
Affinity.assignToProcessor(procToBind, thread);
Affinity.printSituation(); // now you see it there...
// start the thread!
thread.start();
Affinity.printSituation(); // now it is running with a pid...
}
}
Output:
$ java -cp coralthreads-all.jar com.coralblocks.coralthreads.sample.Basics 2
CpuInfo: [nChips=1, nCoresPerChip=4, hyper-threading=true, nProcessors=8, procIds=0,1,2,3,4,5,6,7]
Chip-0:
Core-0:
Processor-0: free
Processor-4: free
Core-1:
Processor-1: free
Processor-5: free
Core-2:
Processor-2: free
Processor-6: free
Core-3:
Processor-3: free
Processor-7: free
CpuInfo: [nChips=1, nCoresPerChip=4, hyper-threading=true, nProcessors=8, procIds=0,1,2,3,4,5,6,7]
Chip-0:
Core-0:
Processor-0: free
Processor-4: free
Core-1:
Processor-1: free
Processor-5: free
Core-2:
Processor-2: assigned to MyPinnedThread (not-started)
Processor-6: free
Core-3:
Processor-3: free
Processor-7: free
CpuInfo: [nChips=1, nCoresPerChip=4, hyper-threading=true, nProcessors=8, procIds=0,1,2,3,4,5,6,7]
Chip-0:
Core-0:
Processor-0: free
Processor-4: free
Core-1:
Processor-1: free
Processor-5: free
Core-2:
Processor-2: bound to MyPinnedThread (running pid=2180)
Processor-6: free
Core-3:
Processor-3: free
Processor-7: free