Thread.setPriority outside of run method

Discussion in 'Java' started by grk@usa.net, Jun 16, 2005.

  1. Guest

    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.
    , Jun 16, 2005
    #1
    1. Advertising

  2. Vova Reznik Guest

    wrote:
    > 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
    Vova Reznik, Jun 16, 2005
    #2
    1. Advertising

  3. Lucy Guest

    <> wrote in message
    news:...
    > 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.
    Lucy, Jun 16, 2005
    #3
  4. Roland Guest

    On 16-6-2005 20:01, wrote:

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

    Roland de Ruiter
    ` ___ ___
    `/__/ w_/ /__/
    / \ /_/ / \
    Roland, Jun 16, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeffrey Barish
    Replies:
    0
    Views:
    302
    Jeffrey Barish
    May 28, 2004
  2. Replies:
    5
    Views:
    1,605
    Roedy Green
    Jun 20, 2008
  3. KevinSimonson
    Replies:
    7
    Views:
    347
    Screamin Lord Byron
    Oct 18, 2010
  4. Krzysztof Poc

    outside type, outside function

    Krzysztof Poc, Feb 3, 2012, in forum: C++
    Replies:
    1
    Views:
    282
    Victor Bazarov
    Feb 7, 2012
  5. Prashant

    stop thread from outside 'run'

    Prashant, Jun 5, 2012, in forum: Python
    Replies:
    0
    Views:
    273
    Prashant
    Jun 5, 2012
Loading...

Share This Page