Results 1 to 4 of 4
- 12-14-2010, 01:35 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
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!
Most of the outputs are like: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"); } } }
Sometimes it outputs:Java Code:low priority started high priority started low priority running. high priority running. low priority stopped. high priority stopped.
that the high priority runs before the low priority, which should happen all the time. :confused:Java Code:low priority started high priority started high priority running. low priority running. low priority stopped. high priority stopped.
- 12-14-2010, 01:36 PM #2
- 12-14-2010, 01:51 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
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:
- 02-01-2011, 08:06 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
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
-
priority
By simorgh in forum Threads and SynchronizationReplies: 4Last Post: 01-07-2012, 12:49 AM -
peemptive priority
By IT student in forum New To JavaReplies: 4Last Post: 04-09-2010, 02:52 PM -
Setting up proxy for Maven mojo doesn't work
By zdenek.zikan in forum Advanced JavaReplies: 1Last Post: 01-12-2010, 12:14 PM -
Priority Queue Question
By Taz_84 in forum New To JavaReplies: 0Last Post: 01-29-2009, 03:23 AM -
How to get/set thread priority
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks