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

前端 未结 3 1336
醉话见心
醉话见心 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 12:02

    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
    

提交回复
热议问题