This documentation is archived and is not being maintained.

java.lang.Thread.setPriority(int priority)

Visual Studio 2005

Sets the priority of the current thread.


public final void setPriority(int priority)

Parameters

Parameter Description

priority

A value indicating the priority for the current thread.

The default priority for a thread is defined by 31c5b51c-29cd-4f46-a943-6275f0acf0a2. The maximum priority for a thread is defined by 956ddc7b-7eb0-4857-a442-fd0d6d2d88b4. The minimum priority for a thread is defined by f8387b47-a01d-4f34-8d67-10e44c6c24e6.

// thread_setpriority.jsl

public class Program implements Runnable
{
    public static void main(String[] args)
    {
        Program p = new Program();
        p.init();
    }

    public void init()
    {
        try
        {
            // Create a second thread and start it.
            Thread thr2 = new Thread(this);
            thr2.setPriority(Thread.MAX_PRIORITY);
            System.out.println("Starting " +
                thr2.getName() + "...");
            thr2.start();

            // Create a third thread and start it.
            Thread thr3 = new Thread(this);
            thr3.setPriority(Thread.NORM_PRIORITY + 1);
            System.out.println("Starting " +
                thr3.getName() + "...");
            thr3.start();

            // Display the number of active threads.  This
            // count includes the main thread.
            System.out.println("Active threads = " +
                Thread.activeCount());

            // Block until the other threads finish.
            thr2.join();
            thr3.join();
        }
        catch (InterruptedException ex)
        {
            System.out.println(ex.toString());
        }
    }

    // Implements Runnable.run()
    public void run()
    {
        for (int i = 0; i < 100000000; i++)
        {
            // Just increment a counter.
            counter++;
        }

        // Display the name of the thread executing this code.
        System.out.println(Thread.currentThread().getName() +
            " [priority = " + Thread.currentThread().getPriority() +
            "] has finished executing.");
    }

    private int counter = 0;
}

/*
Sample Output:
Starting Thread-0...
Starting Thread-1...
Active threads = 3
Thread-0 [priority = 10] has finished executing.
Thread-1 [priority = 6] has finished executing.
*/
Show: