Do threads work with respect to their respective priority number?

Why would the compiler print 2 a and then 2 b or vice versa when giving the priority to Thread a to start? Shouldn't thread b wait for thread a to finish in order to start? Can someone please explain how does it work?

public class Test1 extends Thread{
    static int x = 0;
    String name;

    Test1(String n) {
      name = n;

    public void increment() {
        x = x+1;
        System.out.println(x + " " + name);

    public void run() {

public class Main {
    public static void main(String args[]) {
        Test1 a = new Test1("a");
        Test1 b = new Test1("b");

Giving priorities is not a job for the compiler. It is the OS scheduler to schedule and give CPU time (called quantum) to threads.

The scheduler further tries to run as much threads at once as possible, based on the available number of CPUs. In today's multicore systems, more often than not more than one core are available.

If you want for a thread to wait for another one, use some synchronizing mechanism.

Windows never adjusts the priority of threads in the real-time range (16 through 31), so they always have the same base and current priority. A thread’s initial base priority is inherited from the process base priority. A process, by default, inherits its base priority from the process that created it.

Shouldn't thread b wait for thread a to finish in order to start?

No. The priority does not block the thread execution. It only tells the JVM to execute the thread "in preference to threads with lower priority". This does imply a wait.

Since your code is so trivial, there is nothing to wait for. Any of the two threads is run.

By default, Work Managers have equal priority with the scheduler, with a share value of 50. When the scheduler attempts to execute waiting requests, it ensures that requests associated with each Work Manager are given an equal number of thread resources (assuming an equal number of waiting requests).

Why would the compiler print 2 a and then 2 b?

Luck of the draw. "Priority" means different things on different operating systems, but in general, it's always part of how the OS decides which thread gets to run and which one must wait when there's not enough CPUs available to run them both at the same time. If you computer has two or more idle CPUs when you start that program, then everybody gets to run. Priority doesn't matter in that case, and its just a race to see which one gets to the println(...) call first.

The a thread in your example has an advantage because the program doesn't call b.start() until after the a.start() method returns, but how big that advantage actually is depends on the details of the OS thread scheduling algorithm. The a thread could get a huge head start (like, it actually finishes before b even starts), or it could be a near-trivial head start.

a thread causes a trap or exception may need to choose a different thread/process to run • We glossed over the choice of which process or thread is chosen to be run next “some thread from the ready queue” • This decision is called scheduling o scheduling is a policy o context switching is a mechanism

The many-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads, combining the best features of the one-to-one and many-to-one models. Users have no restrictions on the number of threads created. Blocking kernel system calls do not block the entire process.

Threads are not independent of one other like processes as a result threads shares with other threads their code section, data section, OS resources also known as task, such as open files and signals. Processes Vs Threads. As we mentioned earlier that in many respect threads operate in the same way as that of processes.

void QThread:: setPriority (QThread::Priority priority) This function sets the priority for a running thread. If the thread is not running, this function does nothing and returns immediately. Use start() to start a thread with a specific priority. The priority argument can be any value in the QThread::Priority enum except for InheritPriority.