EaGle Network
EaGle Network

Reputation: 103

Thread priority not working properly. What wrong i have done in my code?

So I have created two classes, MyThread1 and MyThread2, which are extending the Thread class.

public class MyThread1 extends Thread{

    public void run() {
        System.out.println("MyThread1 output");
    }
}

public class MyThread2 extends Thread{

    public void run() {
        System.out.println("MyThread2 output");
    }
}

public class Main {

    public static void main(String[] args) {

        MyThread1 mt1 = new MyThread1();
        MyThread2 mt2 = new MyThread2();

        mt1.setPriority(10);

        mt1.start();
        mt2.start();
        System.out.println("aaaaaa");
    }
}
#

Now according to my program MyThread1 should run before MyThread2 because I have set its priority to 10, but still I am getting this output:

Output:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
#

Please someone tell me why Mythread2 is printed first then MyThread1 even thought MyThread1 has high priority.

Upvotes: 0

Views: 466

Answers (2)

nityanarayan
nityanarayan

Reputation: 378

Just adding on the @htpvl,

In Java, Thread has its own int type variables for the priority values as (MAX_PRIORITY, NORM_PRIORITY, MIN_PRIORITY)

you can make use of that as @htpvl explained.

You can also play around its value like increasing or decreasing.
For Example: Lets say you are having 5 thread, and you want to assign the priority accordingly, then what you can do is:

// Creating instances
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
Thread t3 = new Thread(r3);
Thread t4 = new Thread(r4);
Thread t5 = new Thread(r5);

// Do check the value of Thread.MAX_PRIORITY, and you can play around it
System.out.println("MAX PRIORITY IS: " + Thread.MAX_PRIORITY); // On Mac it prints : 10
System.out.println("NORMAL PRIORITY IS: " + Thread.NORM_PRIORITY); // On Mac it prints : 5
System.out.println("MIN PRIORITY IS: " + Thread.MIN_PRIORITY); // On Mac it prints : 1

// Set the priority (according to your PRIORITY int values)
t1.setPriority((int)Thread.MAX_PRIORITY-1);
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t4.setPriority((int)Thread.NORM_PRIORITY +1);
t5.setPriority((int)Thread.NORM_PRIORITY +2);

// Starting the threads [Execution]
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();

Output for Thread Sequence:

t2
t1
t5
t4
t3

Upvotes: 1

Tran Ho
Tran Ho

Reputation: 1500

Regarding your problem, I guess you don't save your code before executing. Make sure it's saved and execute again.

When two thread 1 & 2 are created, their priorities inherit from main thread. Let says if main thread has priority is 5, they will be 5. When you set priority of thread 1 is 10 (max priority). The result should be:

Thread 1....
Thread 2 or Main thread ( because of same priority)

I have attached my sample code:

     Runnable r1 = new Runnable() {
        public void run() {
            System.out.println("Runnable 1");
        }
    };
    Runnable r2 = new Runnable() {
        public void run() {
            System.out.println("Runnable 2");
        }
    };

    Thread t1 = new Thread(r1);
    Thread t2 = new Thread(r2);
    t2.setPriority(Thread.MAX_PRIORITY);
    t1.start();
    t2.start();

    System.out.println("Main thread");

Output :

Runnable 2
Runnable 1
Main thread

Upvotes: 1

Related Questions