Thread setPriority(int) in Java

setPriority(int): This method is available in the java.lang.Thread class of Java.

Syntax:

void java.lang.Thread.setPriority(int newPriority)

This method takes one argument. This method changes the priority of this thread.

Parameters: One parameter is required for this method.

newPriority: priority to set this thread too.

Returns: NA

Throws:

1. IllegalArgumentException - If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.

2. SecurityException - if the current thread cannot modify this thread.

Approach 1: When no exception

Java

package com.Thread;

public class ThreadsetPriority {
    public static void main(String[] args) {

        Thread thread = new Thread();

        int newPriority = Thread.MAX_PRIORITY;
        thread.setPriority(newPriority);
        System.out.println("setPriority");
    }
}

Output:

setPriority


Approach 2: IllegalArgumentException 

Java

package com.Thread;

public class ThreadsetPriority {
    public static void main(String[] args) {

        Thread thread = new Thread();

        int newPriority = 100;
        thread.setPriority(newPriority);
        System.out.println("setPriority");
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException at java.base/java.lang.Thread.setPriority(Thread.java:1137) at com.Thread.ThreadsetPriority.main(ThreadsetPriority.java:9)


Some other methods of Thread

getUncaughtExceptionHandler()This method returns the handler invoked when this thread abruptly terminates due to an uncaught exception.

Thread.holdsLock(Object)This method returns true if and only if the current thread holds the monitor lock on the specified object.

interrupt()This method interrupts this thread.

Thread.interrupted()This method tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method.

isAlive()This method tests if this thread is alive. A thread is alive if it has been started and has not yet died.

isDaemon()This method tests if this thread is a daemon thread.

isInterrupted()This method tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method.

join()This method waits for this thread to die.

join(long)This method waits at most millis milliseconds for this thread to die. A timeout of 0 means waiting forever.

join(long, int)This method waits at most millis milliseconds plus nanos nanoseconds for this thread to die. If both arguments are 0, it means to wait forever.

Thread.onSpinWait()This method indicates that the caller is momentarily unable to progress, until the occurrence of one or more actions on the part of other activities.

run()If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

setContextClassLoader(ClassLoader)This method sets the context ClassLoader for this Thread.

setDaemon(boolean)This method marks this thread as either a daemon thread or a user thread.

setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler)This method set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread.

setName(String)This method changes the name of this thread to be equal to the argument name.

setPriority(int)This method changes the priority of this thread.

setUncaughtExceptionHandler(UncaughtExceptionHandler)This method set the handler invoked when this thread abruptly terminates due to an uncaught exception.

Thread.sleep(long)This method causes the currently executing thread to sleep for a specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

Thread.sleep(long, int)This method causes the currently executing thread to sleep for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.

start()This method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

toString()This method returns a string representation of this thread, including the thread's name, priority, and thread group.

Thread.yield()This method is a hint to the scheduler that the current thread is willing to yield its current use of a processor. 

No comments:

Post a Comment