Results 1 to 4 of 4
  1. #1
    ggyyree is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Question Why the priority setting doesn't work?

    Why the priority setting doesn't work? I set the threads with different priority, but it doesn't work. Any suggestions? Thanks!

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package mythreadthree;
    
    /**
     *
     * @author guangyan
     */
    public class MyThreadThree implements Runnable {
    
      Thread t;
      private volatile boolean running = true;
    
      public MyThreadThree (int p, String tName) {
         t = new Thread(this,tName);
         t.setPriority (p);
      }
      
      public void run() {
         System.out.println(t.getName() + " running.");
      }
    
      public void stop() {
         running = false;
         System.out.println(t.getName() + " stopped.");
      }
    
      public void start() {
         System.out.println(t.getName() + " started");
         t.start();
      }
    
      public static void main( String args[] ) {
    
         Thread.currentThread().setPriority(10);
    
         MyThreadThree lowPriority  = new MyThreadThree (3, "low priority");
    
         MyThreadThree highPriority = new MyThreadThree (7, "high priority");
    
         lowPriority.start();
         highPriority.start();
    
         try {
            Thread.sleep(1000);
         } catch ( InterruptedException e) {
            System.out.println("Main thread interrupted.");
         }
    
         lowPriority.stop();
         highPriority.stop();
    
         try {
            highPriority.t.join();
            lowPriority.t.join();
         } catch (InterruptedException e) {
              System.out.println(
                      "InterruptedException caught");
         }
    
      }
    }
    Most of the outputs are like:

    Java Code:
    low priority started
    high priority started
    low priority running.
    high priority running.
    low priority stopped.
    high priority stopped.
    Sometimes it outputs:

    Java Code:
    low priority started
    high priority started
    high priority running.
    low priority running.
    low priority stopped.
    high priority stopped.
    that the high priority runs before the low priority, which should happen all the time. :confused:

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    Quote Originally Posted by ggyyree View Post
    that the high priority starts before the low priority, which should happen all the time. :confused:
    What makes you believe that this is true?

  3. #3
    ggyyree is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    What makes you believe that this is true?
    Because that's why we set the priority, right? If even we set the priority, the threads still start and finish randomly. Then why we need the set priority method?

    The thread which set to higher priority method doesn't have the high priority, and it doesn't make sense. :confused::confused:

  4. #4
    golan2 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Here is what I know about threads and Java.
    I hope I am correct…

    1. Java Spec does not tell the JVM vendors how to implement the thread scheduling. You can read The JavaTM Virtual Machine Specification for details.

    2. If you have more than a single CPU then maybe your JVM uses both of them to run the threads and then since the low started first it finishes first?
    And maybe your JVM doesn’t implement priority at all…

    3. I think that the “round-robin” (ie: no priority) is the common implementation in JVMs but it doesn’t matter because if you want a portable code you won’t count on priority. Read more here.

    Now regarding your code:
    I believe that your “run” method is too short.
    When you start the low priority thread it performs it’s “run” and finishes before you start the other high thread.

    IMO: If I were you I would use 3 threads.
    One thread (priority=high) which is started first and 2 other threads (priority=medium, priority=low) started later while the first is running and are waiting for the first to finish.
    When the running thread finishes the JVM will have to choose one of the others to run.
    If you JVM implements priority then he will choose the medium over the low.

    Hope it helped,

Similar Threads

  1. priority
    By simorgh in forum Threads and Synchronization
    Replies: 4
    Last Post: 01-07-2012, 01:49 AM
  2. peemptive priority
    By IT student in forum New To Java
    Replies: 4
    Last Post: 04-09-2010, 03:52 PM
  3. Setting up proxy for Maven mojo doesn't work
    By zdenek.zikan in forum Advanced Java
    Replies: 1
    Last Post: 01-12-2010, 01:14 PM
  4. Priority Queue Question
    By Taz_84 in forum New To Java
    Replies: 0
    Last Post: 01-29-2009, 04:23 AM
  5. How to get/set thread priority
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •