Java 最小优先级线程如何在最大优先级线程之前执行?

Java 最小优先级线程如何在最大优先级线程之前执行?,java,multithreading,Java,Multithreading,在执行这段代码时,有时我注意到具有最小优先级的线程在具有最大优先级的线程之前执行。有人能帮我理解这里发生了什么吗 class Test implements Runnable { public void run() { for(int i=0; i<5; i++) System.out.println(Thread.currentThread().getName()); } } class TestMain { pu

在执行这段代码时,有时我注意到具有最小优先级的线程在具有最大优先级的线程之前执行。有人能帮我理解这里发生了什么吗

class Test implements Runnable 
{
    public void run()
    {
        for(int i=0; i<5; i++)
            System.out.println(Thread.currentThread().getName());
    }
}
class TestMain
{
    public static void main(String[] args)
    {
        Test test = new Test();
        Thread t1 = new Thread(test);
        Thread t2 = new Thread(test);

        t1.setPriority(Thread.MAX_PRIORITY);
        t1.setName("Max priority thread");

        t2.setPriority(Thread.MIN_PRIORITY);
        t2.setName("Min priority thread");

        t1.start();
        t2.start();
    }
}    
有时也像

Max priority thread
Max priority thread
Max priority thread
Max priority thread
Max priority thread
Min priority thread
Min priority thread
Min priority thread
Min priority thread
Min priority thread

从逻辑上讲,具有最大优先级的线程应该首先启动。但有时情况并非如此。如果这是不可避免的,那么如何确保具有最高优先级的线程始终首先启动?

尝试使用ExecutorServiceForkJoinPool以更好地控制线程

希望这能帮助你:

java.util.List<Thread> threads = new ArrayList<>();
for(int i = 0; i < 10; i++){
    threads.add(i, new Thread());
}
for(Thread thread : threads){
    thread.start();
    thread.wait(10);
}

尝试使用ExecutorServiceForkJoinPool以更好地控制线程

希望这能帮助你:

java.util.List<Thread> threads = new ArrayList<>();
for(int i = 0; i < 10; i++){
    threads.add(i, new Thread());
}
for(Thread thread : threads){
    thread.start();
    thread.wait(10);
}


优先级只是对调度程序的一个建议。确保一个特定线程先于另一个线程执行的唯一方法是使用适当的并发机制(如优先级)自己执行。

优先级只是对调度程序的建议。确保一个特定线程先于另一个线程执行的唯一方法是使用适当的并发机制(例如,.

线程不需要根据其优先级执行)。最终由操作系统决定何时执行哪个线程。您给出的优先级只是一个建议。我认为线程优先级只是对底层操作系统的一个建议。无法保证哪个线程将首先执行。@最重要的是:那么有没有任何方法或算法可以确保某个特定线程将首先执行?@NullPointer-我不这么认为。您可以让低优先级线程等待高优先级线程发信号的同步对象,但我们可能如果你告诉我们你的用例,你会帮你更好。当使用多个线程时,它们执行的顺序很少有关系。如果需要,则不需要线程。线程不需要根据优先级执行。最终由操作系统决定何时执行哪个线程。您给出的优先级只是一个建议。我认为线程优先级只是对底层操作系统的一个建议。无法保证哪个线程将首先执行。@最重要的是:那么有没有任何方法或算法可以确保某个特定线程将首先执行?@NullPointer-我不这么认为。您可以让低优先级线程等待高优先级线程发信号的同步对象,但我们可能如果你告诉我们你的用例,你会帮你更好。当使用多个线程时,它们执行的顺序很少有关系。如果需要,则不需要线程。不知道
CountDownLatch
。这真的很有帮助。谢谢。我不知道倒计时闩锁。这真的很有帮助。谢谢。这有帮助吗?是的。谢谢你再给我提一个建议。@这有帮助吗?是的。谢谢你再给我一个建议。
    public void start() {
        final int parallelism = Runtime.getRuntime().availableProcessors();
        final ForkJoinPool forkJoinPool = new ForkJoinPool(parallelism);
        final List<ForkJoinTask<Thread>> tasks = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            tasks.add(i, newTask(forkJoinPool, i));
            tasks.get(i).invoke().start();
        }
        forkJoinPool.shutdown();
    }

    private ForkJoinTask<Thread> newTask(final ForkJoinPool pool, final int i) {
        return pool.submit(() -> new Thread(() -> {
            System.out.println("no " + i);
            Thread.sleep(50);
        })).fork();
    }
no 0
no 1
no 2
no 3
no 4
no 5
no 6
no 7
no 8
no 9
no 10
no 12
no 11
no 13
no 14
no 15
no 16
no 17
no 18
no 19
no 20
no 21
no 22
no 23
no 24
no 25
no 26
no 27
no 28
no 29
no 30
no 31
no 32
no 34
no 37
no 38
no 36
no 33
no 35
no 40
no 39
no 41
no 42
no 43
no 44
no 45
no 46
no 47
no 48
no 49
no 50
no 51
no 52
no 53
no 54
no 55
no 56
no 57
no 58
no 59
no 61
no 60
no 62
no 63
no 64
no 65
no 66
no 67
no 69
no 68
no 70
no 72
no 71
no 73
no 74
no 75
no 76
no 78
no 77
no 79
no 80
no 81
no 82
no 84
no 83
no 85
no 86
no 87
no 88
no 89
no 90
no 91
no 92
no 93
no 94
no 96
no 95
no 97
no 98
no 99