Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Thread.setPriority outside of run method

Thread view: 
grk@usa.net - 16 Jun 2005 19:01 GMT
If I call setPriority in the constructor of a class that extends Thread
isn't that going to set the priority of the main thread, since the new
thread has not started?

I can't find this in the javadocs.
Vova Reznik - 16 Jun 2005 19:11 GMT
> If I call setPriority in the constructor of a class that extends Thread
> isn't that going to set the priority of the main thread, since the new
> thread has not started?
>
> I can't find this in the javadocs.

setPriority isn't static
Lucy - 16 Jun 2005 19:13 GMT
> If I call setPriority in the constructor of a class that extends Thread
> isn't that going to set the priority of the main thread, since the new
> thread has not started?
>
> I can't find this in the javadocs.

I can get a list of all existing threads. I can get their names, their
priorities,
if they are daemon threads or not etc.
To change a threads priority you have to specify which one. I don't see how
it matters if the thread has been started or not.
Roland - 16 Jun 2005 21:31 GMT
> If I call setPriority in the constructor of a class that extends Thread
> isn't that going to set the priority of the main thread, since the new
> thread has not started?
>
> I can't find this in the javadocs.

setPriority only affects the priority of the Thread instance on which
the method was called. It does not change the priority of the thread
that is executing the setPriority method, unless of course, the
executing thread is the same thread as the Thread instance on which the
method was called.

It doesn't matter if the Thread instance is "new" (i.e. just created, or
being created) or if the Thread instance is already running.

The following class shows your example case:

public class MyThread extends Thread {
   public MyThread(int prio, Runnable target) {
      super(target); // invoke constructor of parent
      this.setPriority(prio); // change priority *in constructor*
   }
   private static void print(String msg, Thread t) {
      System.out.print(msg);
      System.out.print("\t prio=");
      System.out.print(t.getPriority());
      System.out.print("\t alive=");
      System.out.print(t.isAlive());
      System.out.println();
   }
   public static void main(String[] args) throws Exception {
      final Thread mainThread = Thread.currentThread();
      final MyThread myThread = new MyThread(Thread.MIN_PRIORITY,
            new Runnable() {
               public void run() {
                  print("Running\t main thread", mainThread);
                  print("Running\t my thread", Thread.currentThread());
               }
            });
      print("Before\t main thread", mainThread);
      print("Before\t my thread", myThread);

      myThread.start(); // starts myThread
      myThread.join(); // wait until myTread ends

      print("After\t main thread", mainThread);
      print("After\t my thread", myThread);
   }
}
Signature

Regards,

Roland de Ruiter
` ___      ___
`/__/ w_/ /__/
/  \ /_/ /  \



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.