Results 1 to 5 of 5

Thread: Thread priority

  1. #1
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Thread priority

    Hello.
    Could someone please explain the below code to me? Nothing difficult, but i cant get my head around it.. Why does the output look like this:

    Java Code:
    6
    54 13 10 
    5
    53 13 10 
    3
    51 13 10 
    
    13 10 
    
    13 10 
    3
    51 13 10

    Java Code:
    class PriorityDemo
    {
       public static void main (String [] args)
       {
          BlockingThread bt = new BlockingThread ();
          bt.setPriority (Thread.NORM_PRIORITY + 1);
          CalculatingThread ct = new CalculatingThread ();
          bt.start ();
          ct.start ();
          try
          {
              Thread.sleep (10000);
          }
          catch (InterruptedException e)
          {
          }
          bt.setFinished (true);
          ct.setFinished (true);
       }
    }
    class BlockingThread extends Thread
    {
       private boolean finished = false;
       public void run ()
       {
          while (!finished)
          {
             try
             {
                int i;
                do
                {
                   i = System.in.read ();
                   System.out.print (i + " ");
                }
                while (i != '\n');
                System.out.print ('\n');
             }
             catch (java.io.IOException e)
             {
             }
          }
       }
       public void setFinished (boolean f)
       {
          finished = f;
       }
    }
    class CalculatingThread extends Thread
    {
       private boolean finished = false;
       public void run ()
       {
          int sum = 0;
          while (!finished)
             sum++;
       }
       public void setFinished (boolean f)
       {
          finished = f;
       }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Thread priority

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Thread priority

    My topic got moved, i feel more skilled already
    Back to the point - the output, as far as i have understood..:
    The main thread starts 2 new thread, a blocking (higher priority than the other two) thread and a calculation thread and goes to sleep.
    The blocking thread then goes into action, asking for user input (blocking itself,and then calc thread runs if the main is still sleeping?). Afterwards prints someoutput which i cant understand: i enter 5, but the printed info is: 53 12 10.. why?
    The threads keep running till main wakes and edits the finished variables so that the other two threads stop. Am i getting this right?
    Could someone please explain a bit more? Thanks!

  4. #4
    Potato is offline Member
    Join Date
    Dec 2011
    Posts
    25
    Rep Power
    0

    Default Re: Thread priority

    If you use System.in.read(), you're reading characters one at a time.

    So the number '5' actually has the numeric value 53 (look at an ASCII table like this: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion)

    If you enter 5, this is what System.in.read() will give you. 3 characters, they are:

    5\r\n
    That's the character 5, a carriage return, and a new line, otherwise known as:

    53 13 10
    What you want to do is use char instead of int. Maybe this:

    Java Code:
    class BlockingThread extends Thread
    {
       private boolean finished = false;
       public void run ()
       {
          while (!finished)
          {
             try
             {
                char c;
                do
                {
                   c = (char) System.in.read ();
                   System.out.print (c + " ");
                }
                while (c != '\n');
                System.out.print ('\n');
             }
             catch (java.io.IOException e)
             {
             }
          }
       }
       public void setFinished (boolean f)
       {
          finished = f;
       }
    }

  5. #5
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 04-16-2012, 10:40 AM
  2. Priority Queue with explicit priority
    By lsk in forum Advanced Java
    Replies: 4
    Last Post: 06-10-2011, 07:16 PM
  3. need help with low priority thread to display time
    By vkokaram in forum New To Java
    Replies: 6
    Last Post: 07-19-2010, 01:10 AM
  4. how high-priority thread allow other thread
    By rameshkr in forum Threads and Synchronization
    Replies: 4
    Last Post: 10-15-2009, 10:46 PM
  5. How to get/set thread priority
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06: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
  •