Siddharth M
Siddharth M

Reputation: 157

How is minimum priority thread getting executed before maximum priority thread?

While executing this code, sometimes I notice that the thread with minimum priority is getting executed before the thread with maximum priority. Can anyone help me understand what's going on here?

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();
    }
}    

Sometimes, I'm getting the output as

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

and sometimes as

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

Logically, the thread with maximum priority should start first. But sometimes it is not happening. If this is unavoidable, then how to make sure that the thread with maximum priority will always start first?

Upvotes: 0

Views: 1376

Answers (2)

AvB
AvB

Reputation: 171

try using ExecutorService or ForkJoinPool in order to better control on your threads.

and may this will help you :

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);
}

EDIT: example for ForkJoinPool:

    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();
    }

and the output:

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

Upvotes: 2

Ian Roberts
Ian Roberts

Reputation: 122374

Priorities are just a suggestion to the scheduler. The only way to guarantee that one particular thread executes before another is to do it yourself with an appropriate concurrency mechanism such as a CountDownLatch.

Upvotes: 2

Related Questions