- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - ThreadGroup getMaxPriority() Method
Description
The Java ThreadGroup getMaxPriority() method returns the maximum priority of this thread group. Threads that are part of this group cannot have a higher priority than the maximum priority.
Declaration
Following is the declaration for java.lang.ThreadGroup.getMaxPriority() method
public final int getMaxPriority()
Parameters
NA
Return Value
This method returns the maximum priority that a thread in this thread group can have.
Exception
NA
Getting Max Priority of a ThreadGroup Object Example
The following example shows the usage of ThreadGroup getMaxPriority() method in case of a single ThreadGroup object. We've created a ThreadGroup object and assigned it a name. Then we've created two threads using the threadgroup object created earlier. Using getMaxPriority() method, we're getting all the maximum priority of the thread group object.
package com.tutorialspoint;
public class ThreadGroupDemo implements Runnable {
public static void main(String[] args) {
ThreadGroupDemo tg = new ThreadGroupDemo();
tg.start();
}
public void start() {
try {
// create a ThreadGroup
ThreadGroup threadGroup = new ThreadGroup("ThreadGroup");
// create a thread
Thread t1 = new Thread(threadGroup, this);
System.out.println("Starting " + t1.getName() + "...");
t1.start();
// create another thread
Thread t2 = new Thread(threadGroup, this);
System.out.println("Starting " + t2.getName() + "...");
t2.start();
// returns the maximum priority of thread group
int i = threadGroup.getMaxPriority();
System.out.println("Maximum priority of threadGroup =" + i);
// block until the other threads finish
t1.join();
t2.join();
} catch (InterruptedException ex) {
System.out.println(ex.toString());
}
}
// implements run()
public void run() {
for(int i = 0; i < 4;i++) {
i++;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " finished executing.");
}
}
Output
Let us compile and run the above program, this will produce the following result −
Starting Thread-0... Starting Thread-1... Maximum priority of threadGroup =10 Thread-0 finished executing. Thread-1 finished executing.
Getting Max Priority of a ThreadGroup in Multiple ThreadGroup Objects Example
The following example shows the usage of ThreadGroup getMaxPriority() method in case of multiple ThreadGroup objects. We've created a ThreadGroup object and assigned it a name. As next, we've created a child ThreadGroup object. Then we've created two threads using the threadgroup objects created earlier. Using getMaxPriority() method, we're printing max priorities of each threadgroup object.
package com.tutorialspoint;
public class ThreadGroupDemo implements Runnable {
public static void main(String[] args) {
ThreadGroupDemo tg = new ThreadGroupDemo();
tg.start();
}
public void start() {
try {
// create a parent ThreadGroup
ThreadGroup pThreadGroup = new ThreadGroup("parent ThreadGroup");
// create a child ThreadGroup for parent ThreadGroup
ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "child ThreadGroup");
// create a thread
Thread t1 = new Thread(pThreadGroup, this);
System.out.println("Starting " + t1.getName() + "...");
t1.start();
// create another thread
Thread t2 = new Thread(cThreadGroup, this);
System.out.println("Starting " + t2.getName() + "...");
t2.start();
// returns the maximum priority of thread group
int i = pThreadGroup.getMaxPriority();
System.out.println("Maximum priority of pThreadGroup =" + i);
i = cThreadGroup.getMaxPriority();
System.out.println("Maximum priority of cThreadGroup =" + i);
// block until the other threads finish
t1.join();
t2.join();
} catch (InterruptedException ex) {
System.out.println(ex.toString());
}
}
// implements run()
public void run() {
for(int i = 0; i < 4;i++) {
i++;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " finished executing.");
}
}
Output
Let us compile and run the above program, this will produce the following result −
Starting Thread-0... Starting Thread-1... Maximum priority of pThreadGroup =10 Maximum priority of cThreadGroup =10 Thread-1 finished executing. Thread-0 finished executing.
Getting Max Priority of a ThreadGroup in Child/GrandChild ThreadGroup Objects Example
The following example shows the usage of ThreadGroup getMaxPriority() method in case of child and grandchild ThreadGroup objects. We've created a ThreadGroup object and assigned it a name. As next, we've created a child ThreadGroup object. Then we've created two threads using the child and grandchild threadgroup objects created earlier. Using getMaxPriority() method, we're printing max priorities of each threadgroup object.
package com.tutorialspoint;
public class ThreadGroupDemo implements Runnable {
public static void main(String[] args) {
ThreadGroupDemo tg = new ThreadGroupDemo();
tg.start();
}
public void start() {
try {
// create a parent ThreadGroup
ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
// create a child ThreadGroup for parent ThreadGroup
ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");
// create a grandchild ThreadGroup for parent ThreadGroup
ThreadGroup gThreadGroup = new ThreadGroup(cThreadGroup, "GrandChild ThreadGroup");
// create a thread
Thread t1 = new Thread(cThreadGroup, this);
System.out.println("Starting " + t1.getName() + "...");
t1.start();
// create another thread
Thread t2 = new Thread(gThreadGroup, this);
System.out.println("Starting " + t2.getName() + "...");
t2.start();
// returns the maximum priority of thread group
int i = pThreadGroup.getMaxPriority();
System.out.println("Maximum priority of pThreadGroup =" + i);
i = cThreadGroup.getMaxPriority();
System.out.println("Maximum priority of cThreadGroup =" + i);
i = gThreadGroup.getMaxPriority();
System.out.println("Maximum priority of gThreadGroup =" + i);
// block until the other threads finish
t1.join();
t2.join();
} catch (InterruptedException ex) {
System.out.println(ex.toString());
}
}
// implements run()
public void run() {
for(int i = 0; i < 4;i++) {
i++;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " finished executing.");
}
}
Output
Let us compile and run the above program, this will produce the following result −
Starting Thread-0... Starting Thread-1... Maximum priority of pThreadGroup =10 Maximum priority of cThreadGroup =10 Maximum priority of gThreadGroup =10 Thread-0 finished executing. Thread-1 finished executing.