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












0















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.










share|improve this question

























  • Is this java ?

    – Cid
    Oct 23 '18 at 7:25











  • @Cid yes bro. Do you know the answer

    – EaGle Network
    Oct 23 '18 at 7:58











  • I don't think this would test priority. The tasks are so small, the threads probably don't have any contention. If you run it a bunch, some times 1 is first and sometimes 2 is first.

    – matt
    Oct 23 '18 at 8:21
















0















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.










share|improve this question

























  • Is this java ?

    – Cid
    Oct 23 '18 at 7:25











  • @Cid yes bro. Do you know the answer

    – EaGle Network
    Oct 23 '18 at 7:58











  • I don't think this would test priority. The tasks are so small, the threads probably don't have any contention. If you run it a bunch, some times 1 is first and sometimes 2 is first.

    – matt
    Oct 23 '18 at 8:21














0












0








0








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.










share|improve this question
















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.







java multithreading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 5:07









Mihai Chelaru

2,190101122




2,190101122










asked Oct 23 '18 at 7:18









EaGle NetworkEaGle Network

233




233













  • Is this java ?

    – Cid
    Oct 23 '18 at 7:25











  • @Cid yes bro. Do you know the answer

    – EaGle Network
    Oct 23 '18 at 7:58











  • I don't think this would test priority. The tasks are so small, the threads probably don't have any contention. If you run it a bunch, some times 1 is first and sometimes 2 is first.

    – matt
    Oct 23 '18 at 8:21



















  • Is this java ?

    – Cid
    Oct 23 '18 at 7:25











  • @Cid yes bro. Do you know the answer

    – EaGle Network
    Oct 23 '18 at 7:58











  • I don't think this would test priority. The tasks are so small, the threads probably don't have any contention. If you run it a bunch, some times 1 is first and sometimes 2 is first.

    – matt
    Oct 23 '18 at 8:21

















Is this java ?

– Cid
Oct 23 '18 at 7:25





Is this java ?

– Cid
Oct 23 '18 at 7:25













@Cid yes bro. Do you know the answer

– EaGle Network
Oct 23 '18 at 7:58





@Cid yes bro. Do you know the answer

– EaGle Network
Oct 23 '18 at 7:58













I don't think this would test priority. The tasks are so small, the threads probably don't have any contention. If you run it a bunch, some times 1 is first and sometimes 2 is first.

– matt
Oct 23 '18 at 8:21





I don't think this would test priority. The tasks are so small, the threads probably don't have any contention. If you run it a bunch, some times 1 is first and sometimes 2 is first.

– matt
Oct 23 '18 at 8:21












2 Answers
2






active

oldest

votes


















1














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





share|improve this answer
























  • Thanks for replying buddy. But i made a little research and i found out windows10 doesnt provide support to multithreading. Is it true?

    – EaGle Network
    Oct 23 '18 at 8:10





















1














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





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52943252%2fthread-priority-not-working-properly-what-wrong-i-have-done-in-my-code%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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





    share|improve this answer
























    • Thanks for replying buddy. But i made a little research and i found out windows10 doesnt provide support to multithreading. Is it true?

      – EaGle Network
      Oct 23 '18 at 8:10


















    1














    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





    share|improve this answer
























    • Thanks for replying buddy. But i made a little research and i found out windows10 doesnt provide support to multithreading. Is it true?

      – EaGle Network
      Oct 23 '18 at 8:10
















    1












    1








    1







    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





    share|improve this answer













    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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 23 '18 at 8:04









    htpvlhtpvl

    63739




    63739













    • Thanks for replying buddy. But i made a little research and i found out windows10 doesnt provide support to multithreading. Is it true?

      – EaGle Network
      Oct 23 '18 at 8:10





















    • Thanks for replying buddy. But i made a little research and i found out windows10 doesnt provide support to multithreading. Is it true?

      – EaGle Network
      Oct 23 '18 at 8:10



















    Thanks for replying buddy. But i made a little research and i found out windows10 doesnt provide support to multithreading. Is it true?

    – EaGle Network
    Oct 23 '18 at 8:10







    Thanks for replying buddy. But i made a little research and i found out windows10 doesnt provide support to multithreading. Is it true?

    – EaGle Network
    Oct 23 '18 at 8:10















    1














    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





    share|improve this answer




























      1














      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





      share|improve this answer


























        1












        1








        1







        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 23 '18 at 8:55









        Nityanarayan44Nityanarayan44

        1799




        1799






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52943252%2fthread-priority-not-working-properly-what-wrong-i-have-done-in-my-code%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini