1. join thread :
During thread execution, sometimes you want another thread to execute first, such as dividing a large problem into many small problems, assigning threads to each small problem, but after all small problems are processed, let the main thread perform further operations. At this time, we can call the join() method of other threads in the main thread to block the calling thread (here, the main thread).
Sample code:
Copy the code code as follows:
package org.frzh.thread;
public class JoinThread extends Thread{
//Provide a parameterized constructor to set the name of the thread
public JoinThread(String name) {
super(name);
}
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + " " + i);
}
}
public static void main(String[] args) {
//Start child thread
new JoinThread("new thread").start();
for (int i = 0; i < 100; i++) {
if (i == 20) {
JoinThread jt = new JoinThread("thread to be joined");
jt.start();
//The main thread calls the join method of the jt thread, then the main thread must wait for jt to finish executing before it can execute
try {
jt.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " " +i);
}
}
}
Originally there were three sets of threads (two sub-threads and one main thread). When i=20, the main thread is blocked and has to wait until the "joined thread" is executed before it has a chance to execute, so there are only two threads executed thereafter.
Three overloaded forms of join() method:
join(): wait for the joined thread to finish executing;
join(long millis): The longest time to wait for the joined thread to execute is mills milliseconds. After that, even if the joined thread has not finished executing, it will not wait any longer;
join(long millis, int nanos): The maximum time to wait for the joined thread to execute is millis milliseconds + nanos microseconds. (This method is basically useless).
2: Background thread :
There is a thread that runs in the background, and its task is to serve other threads. This thread is called a "background thread", "daemon thread" or "elf thread". When all foreground threads die, the background thread will die automatically.
Sample code:
Copy the code code as follows:
package org.frzh.thread;
public class DaemonThread extends Thread{
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(getName() + " " +i);
}
}
public static void main(String[] args) {
DaemonThread dt = new DaemonThread();
//Set this thread as a background thread
dt.setDaemon(true);
dt.start();
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
//The foreground thread ends, then the background thread dt will also end, so it will not execute 999
}
}
The main thread defaults to the foreground thread, the sub-threads created by the foreground thread default to the foreground thread, and the sub-threads created by the background thread default to the background thread.
3. Thread sleep (sleep):
The previous join method is to let the calling thread wait for the joined thread to finish executing before continuing, while the sleep() method is to let the calling thread block for a period of time before re-entering the ready state and waiting to be scheduled. Therefore it is often used to pause program execution.
Sample code:
Copy the code code as follows:
package org.frzh.thread;
import java.util.Date;
public class SleepThread{
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("Current time: " + new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Two overloading methods of sleep() method:
static void sleep (long millis): Let the current thread pause for millis milliseconds and enter the blocking state. This method is affected by the precision and accuracy of system timers and thread schedulers.
static void sleep (long millis, int nanos): Pause mills milliseconds + nanos microseconds and enter the blocking state. It will also be affected by the accuracy and accuracy of the system timer and thread scheduler. Basically not needed.
4. Thread yield:
The yield() method is somewhat similar to the sleep method. It can also pause the currently running thread, but it will not block the thread, but just transfer it to the ready state (note that it is not a blocking state). The yield() method will only give threads with the same priority or higher priority a chance to be executed, so a thread may be rescheduled back to continue execution after calling this method.
Sample code:
Copy the code code as follows:
package org.frzh.thread;
public class YieldThread extends Thread{
public YieldThread() {
}
public YieldThread(String name) {
super(name);
}
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + " " +i);
if (i == 20) {
//The current thread yields
Thread.yield();
}
}
}
public static void main(String[] args) {
//Start two concurrent threads
YieldThread yt1 = new YieldThread("Advanced");
//Set yt1 as the highest priority
yt1.setPriority(Thread.MAX_PRIORITY);
yt1.start();
YieldThread yt2 = new YieldThread("low-level");
yt2.setPriority(Thread.MIN_PRIORITY);
yt2.start();
/*
* If the priority is not set for the thread, the priority of the two threads is the same, so the two threads will execute alternately, and when yield is called, the other thread will execute;
* However, after setting the above priorities for the two threads respectively, the advanced thread execution just started. When i=20, yield is called, but because the yield method will only
* Give execution opportunities to threads with the same priority or higher priority, so the high-level thread is still executing at this time and will not be given to low-level threads
*/
}
}
5: Change the priority of the thread :
This is relatively simple, just call the instance method setPriority(int priority) method. Each thread defaults to the same priority as its parent thread, and the main thread defaults to normal priority (5). Java provides priorities from 1 to 10, and you can also use three static constants:
MAX_PRIORITY:10
MIN_PRIORITY:1
NORM_PRIORITY:5
Note: Although Java provides 10 priorities, different systems support different priorities, so try to avoid directly using numbers between 1 and 10, and use static constants to ensure good portability.