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)); } }