Results 1 to 5 of 5
Thread: Thread priority
- 12-07-2012, 01:35 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 38
- Rep Power
- 0
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; } }
- 12-07-2012, 07:46 PM #2
Re: Thread priority
Moved from New to Java.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 12-08-2012, 02:16 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 38
- Rep Power
- 0
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!
- 12-08-2012, 08:42 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 25
- Rep Power
- 0
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
53 13 10
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; } }
- 12-10-2012, 07:53 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
Implementing a Priority queue with two values.Can you store an array in a priority q?
By BetaDave1877 in forum New To JavaReplies: 1Last Post: 04-16-2012, 11:40 AM -
Priority Queue with explicit priority
By lsk in forum Advanced JavaReplies: 4Last Post: 06-10-2011, 08:16 PM -
need help with low priority thread to display time
By vkokaram in forum New To JavaReplies: 6Last Post: 07-19-2010, 02:10 AM -
how high-priority thread allow other thread
By rameshkr in forum Threads and SynchronizationReplies: 4Last Post: 10-15-2009, 11:46 PM -
How to get/set thread priority
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 07:40 PM
Bookmarks