Thread groups
Each Thread must exist in a ThreadGroup. A Thread cannot exist independently of a ThreadGroup.
1.1. Thread group of main method
package co.dianjiu.thread;
public class MyThreadGroup {
public static void main(String[] args) {
// Check the thread group and thread name of the main method
System.out.println("Main method thread group name ===>"+
Thread.currentThread().getThreadGroup().getName());
System.out.println("Main method thread name ===>"+Thread.currentThread().getName()); }}Copy the code
The output
Main Thread group name ===> Main Main thread name ===> Main
1.2. Do not specify thread groups
When a new Thread is not explicitly specified, the parent Thread (the Thread currently executing the new Thread) Thread group is set as its own Thread group by default.
package co.dianjiu.thread;
public class MyThreadGroup {
public static void main(String[] args) {
Thread myThread = new Thread(()->{
System.out.println("MyThread Thread group name ===>"+
Thread.currentThread().getThreadGroup().getName());
System.out.println(MyThread Thread name ===>+Thread.currentThread().getName()); }); myThread.start(); }}Copy the code
The output
MyThread Thread group name ===>main MyThread Thread name ===> thread-0
1.3. How do I specify a thread group
package co.dianjiu.thread;
public class MyThreadGroup {
public static void main(String[] args) {
// Specify a thread group
ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");
Thread myThread = new Thread(threadGroup, new MyRunnableDemo());
myThread.start();
}
public static class MyRunnableDemo implements Runnable{
@Override
public void run(a) {
System.out.println("MyRunnableDemo Thread group name ===>"+
Thread.currentThread().getThreadGroup().getName());
System.out.println("MyRunnableDemo thread name ===>"+ Thread.currentThread().getName()); }}}Copy the code
The output
MyRunnableDemo Thread group name ===>MyThreadGroup MyRunnableDemo Thread name ===>Thread-0
Second, thread priority
In Java, thread priority can be specified in the range of 1 to 10. The default thread priority is 5. The order in which threads are executed is determined by the scheduler, and the priority of threads is set before they are called. In general, threads with higher priority have a higher chance of execution.
2.1. Priority of the main method
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
System.out.println("Main thread priority ===>"+Thread.currentThread().getPriority()); }}Copy the code
The output
Main thread priority ===>5
2.2. How do I set the thread priority
Use setPriority() to set the priority of the thread
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
Thread myThread = new Thread(()->{
System.out.println("MyThread Priority ===>"+
Thread.currentThread().getPriority());
});
myThread.setPriority(8); myThread.start(); }}Copy the code
The output
MyThread Thread priority ===>8
2.3. Must the execution be carried out first if the priority is high
Not necessarily, but the probability of execution is higher, and the final order of execution is determined by the operating system.
package co.dianjiu.thread;
import java.util.stream.IntStream;
public class MyThreadPriority {
public static void main(String[] args) {
IntStream.range(1.10).forEach(i -> {
Thread thread = new Thread(new MyThreadDemo());
thread.setPriority(i);
thread.start();
});
}
public static class MyThreadDemo extends Thread {
@Override
public void run(a) {
super.run();
System.out.println(String.format("Currently executing thread is ===>%s, priority ===>%d", Thread.currentThread().getName(), Thread.currentThread().getPriority())); }}}Copy the code
The output
The current Thread is executing ===> thread-15, priority ===>8 The current Thread is executing ===>Thread-3, priority ===>2 The current Thread is executing ===>Thread-1, priority ===>1 The current Thread is executing ===>Thread-9, priority === =>1 Priority ===>5 Indicates that the currently executing Thread is ===> thread-7. Priority ===>4 Indicates that the currently executing Thread is ===>Thread-17. Priority ===>9 Indicates that the currently executing Thread is ===>Thread-5. Priority = = = > 3 the currently executing Thread is = = = > Thread – 11, priority = = = > 6 the currently executing Thread is = = = > Thread – 13, priority = = = > 7
2.4. How to set the priority of a thread group
Use the setMaxPriority() method
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");
threadGroup.setMaxPriority(6);
System.out.println(MyThreadGroup Thread group priority ===>+threadGroup.getMaxPriority()); }}Copy the code
The output
MyThreadGroup Priority of the thread group ===>6
2.5. Priorities of threads and thread groups are inconsistent
When a thread has a higher priority than a thread group, the thread priority is reset to the thread group priority
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");
// The thread group priority is 6
threadGroup.setMaxPriority(6);
System.out.println(MyThreadGroup Thread group priority ===>+threadGroup.getMaxPriority());
Thread thread = new Thread(threadGroup, new MyThreadDemo());
// The thread priority is 9
thread.setPriority(9);
thread.start();
}
public static class MyThreadDemo extends Thread {
@Override
public void run(a) {
super.run();
System.out.println(String.format("Currently executing thread is ===>%s, priority ===>%d", Thread.currentThread().getName(), Thread.currentThread().getPriority())); }}}Copy the code
The output
MyThreadGroup Thread group priority ===>6 The current Thread is thread-1 and the priority is ===>6
When the priority of a thread is smaller than that of the thread group, the priority of the thread is not reset.
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");
// The thread group priority is 6
threadGroup.setMaxPriority(6);
System.out.println(MyThreadGroup Thread group priority ===>+threadGroup.getMaxPriority());
Thread thread = new Thread(threadGroup, new MyThreadDemo());
// The thread priority is 4
thread.setPriority(4);
thread.start();
}
public static class MyThreadDemo extends Thread {
@Override
public void run(a) {
super.run();
System.out.println(String.format("Currently executing thread is ===>%s, priority ===>%d", Thread.currentThread().getName(), Thread.currentThread().getPriority())); }}}Copy the code
The output
MyThreadGroup Thread group priority ===>6 The current Thread is thread-1 and its priority is ===>4
2.6. How to use daemons
The default priority of daemon threads is low. Use setDaemon(Boolean on) to set this thread to daemon.
If a thread is a daemon thread, then if all non-daemon threads terminate, the daemon thread will terminate automatically.
package co.dianjiu.thread;
import java.util.stream.IntStream;
public class MyThreadPriority {
public static void main(String[] args) {
// The default priority is 5
System.out.println("Main thread priority ===>"+Thread.currentThread().getPriority());
// How to set thread priority
Thread myThread = new Thread(()->{
System.out.println("MyThread Priority ===>"+
Thread.currentThread().getPriority());
});
myThread.setPriority(8);
myThread.start();
// Set the daemon thread
Thread threadDemo = new Thread(()->{
System.out.println("MyThread daemon thread priority ===>"+
Thread.currentThread().getPriority());
});
threadDemo.setDaemon(true); threadDemo.start(); }}Copy the code
The output
Main thread priority ===>5 MyThread Thread priority ===>8 MyThread daemon thread priority ===>5