Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Threads priority: low priority run faster than high priority

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Threads priority: low priority run faster than high priority

    Hello everybody!
    I'm new to Java and I'm reading "Java" written by Herbert Schildt, printed by the McGraw-Hill companies.
    I wrote some classes, like as in the book, to check how my PC runs high/low priority threads.
    I run WinXP and NetBeans IDE 7.0.1, and by the following code, the output is very strange: i try to create two separate threads with different priority, each one counts an integer value adding 1 on every loop; sometimes the low priority thread runs faster than the higher! In my book Mr. Schlidt writes that it shoulds run 10 times slower!
    Please help me, and sorry for my bad english.

    Giulio

    package prioritythreads;
    class clicker implements Runnable {
        int click = 0;
        Thread t;
        private volatile boolean running = true;
     
        public clicker(int p){
            t = new Thread(this);
            t.setPriority(p);
        }
     
        @Override
        public void run(){
            while(running){
                click++;
            }
        }
     
        public void stop(){
            running = false;
        }
     
        public void start(){
            t.start();
        }
    }
     
    public class PriorityThreads {
        public static void main(String[] args) {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            clicker hi = new clicker(Thread.NORM_PRIORITY +4);
            clicker lo = new clicker(Thread.NORM_PRIORITY -4);
     
            hi.start();
            lo.start();
            try{
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("Main Thread interrupted!");
            }
     
            lo.stop();
            hi.stop();
     
            try{
                hi.t.join();
                lo.t.join();
            } catch (InterruptedException e) {
                System.out.println("InterruptedException on Main Thread (join)");
            }
     
            System.out.println("Thread HIGH: " + hi.click);
            System.out.println("Thread LOW: " + lo.click);
            System.out.println("Proportion HIGH/LOW: " + (hi.click/lo.click));
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Threads priority: low priority run faster than high priority

    Threads get distributed across processors, so there are a lot of variables involved - number of processors, how many other processes are swamping those processors, how many threads you have spawned and what their priority is, and how much work your threads are doing. If there is room to distribute threads, they will get distributed. If not, they get squeezed into what is available and distributed by priority. As a test, create four of the high priority and single low priority, then test the output.
    Last edited by copeg; November 8th, 2011 at 12:13 PM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    leonixyz (November 8th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Threads priority: low priority run faster than high priority

    The test works, thank you very much.
    With 2 high priority threads, the one with low priority don't do any loop.

Similar Threads

  1. Priority Queue help
    By BuhRock in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 3rd, 2011, 06:37 PM
  2. generate priority queue from hashmap help
    By Scotty33 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 23rd, 2011, 09:32 PM
  3. Implementing a priority queue using a max heap
    By jsinclair1482 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2011, 11:56 AM
  4. Priority Queue using comparable
    By jkalm in forum Collections and Generics
    Replies: 6
    Last Post: December 5th, 2010, 09:02 PM
  5. Replies: 4
    Last Post: July 21st, 2010, 04:07 PM