start() and run()
explain
The start() method is used to start a thread. When a thread calls the start() method, it means that the thread enters the ready state and waits for thread scheduling
The run() method is just a common method of the Thread class, which holds the work that the Thread needs to complete]
difference
The start() method is to start a thread in real sense, to participate in thread scheduling, and to invoke the run() method does not start a new thread. If the run() method of a thread is invoked in the main thread, the content of the run() method is still the execution of the main thread.
sleep()
explain
The sleep() method will make the current thread enter from the Running state Timed Waiting status
It should be noted that sleep() as a static method of Thread class has nothing to do with who calls it, and will only sleep the current Thread
import lombok.SneakyThrows; public class SleepAndYield { public static void main(String[] args) throws InterruptedException { Thread t = new Thread() { @SneakyThrows @Override public void run() { for(int i = 0; i < 9999999; i ++) { System.out.println(i); } } }; t.start(); t.sleep(5000); } }
In the main thread, t.sleep() is used to make the child thread sleep, but it does not work. T.sleep() here is equivalent to Thread.sleep(), which is used to make the main thread sleep. Therefore, when using sleep(), it is also in the form of Thread.sleep()
In other threads, you can interrupt sleep through the interrupt() method, and the original sleep thread throws an InterruptedException
public class SleepAndYield { public static void main(String[] args) throws InterruptedException { Thread t = new Thread() { @Override public void run() { System.out.println("enter sleep"); try { Thread.sleep(10000); } catch (InterruptedException e) { System.out.println("interrupt success"); e.printStackTrace(); } } }; t.start(); Thread.sleep(1000); System.out.println("interrupt"); t.interrupt(); } }
After an exception is thrown, it is caught by catch for processing
Enhancing the readability of sleep through TimeUnit
Thread sleep is realized through TimeUnit, and the effect is exactly the same as Thread.sleep(), but the readability is greatly enhanced
TimeUnit.DAYS.sleep(1); //day TimeUnit.HOURS.sleep(1); //hour TimeUnit.MINUTES.sleep(1); //branch TimeUnit.SECONDS.sleep(1); //second TimeUnit.MILLISECONDS.sleep(1); //millisecond TimeUnit.MICROSECONDS.sleep(1); //Microsecond TimeUnit.NANOSECONDS.sleep(1); //nanosecond
yield()
The yield() method will make the current thread enter the runnable state from the running state. Unlike sleep(), yield() does not make the thread block waiting, but gives up the CPU time slice currently occupied, but still has the opportunity to occupy the time slice in the next thread scheduling
Similar to sleep(), Thread.yield() is also called
getPriority() and setPriority()
getPriority() is used to obtain thread priority and setPriority() is used to set thread priority. Unlike sleep() and yield(), both methods are called with thread objects
The priority is expressed as an integer of 1 ~ 10. The default priority is 5. The higher the priority, the greater the probability of being allocated to CPU time slice during thread scheduling
join()
The join() method is used to wait for the specified thread to finish running. The parameters that can be passed in represent the maximum waiting time. For example, join(1000) represents the maximum waiting time of 1000 milliseconds. If the thread does not finish running after 1000 milliseconds, the wait will be stopped. If the thread has ended within 1000 milliseconds, if the thread only needs 500 milliseconds to run, the wait will also be stopped instead of waiting until 1000 milliseconds, In particular, join(0) is equivalent to join(), and this method is also called through the thread object
import lombok.SneakyThrows; public class JoinTest { static int value = 0; public static void main(String[] args) throws InterruptedException { System.out.println("start"); Thread t = new Thread() { @SneakyThrows @Override public void run() { System.out.println("start"); sleep(1000); value = 10; System.out.println("end"); } }; t.start();; t.join(); System.out.println("The result is:" + value); System.out.println("end"); } }
In this example, the main thread waits for the end of T thread execution through t.join(), and then outputs the value modified by T thread
interrupt(), isInterrupted(), and interrupted()
For interrupt(), it is used to interrupt the thread
- Interrupt the threads in sleep, wait and join, throw InterruptedException after interruption, and the interrupt flag is cleared by the JVM, that is, call isInterrupted() and the result is false
- Interrupting a normal running thread only marks the interrupt position of the thread as true, that is, the result of calling isInterrupted() is true, and the thread will not be stopped. The user needs to monitor the interrupt flag bit of the thread and handle it
public class InterruptTest { public static void main(String[] args) throws InterruptedException { Thread t = new Thread() { @Override public void run() { while (true) { boolean interrupted = Thread.currentThread().isInterrupted(); if(interrupted) { System.out.println("break"); break; } } } }; t.start(); Thread.sleep(100); System.out.println("interrupt"); t.interrupt(); } }
This example interrupts the t thread in the main thread. The t thread monitors the interrupt flag. Once it is true, it indicates that it receives the interrupt signal and jumps out of the loop
For isInterrupted() and interrupted(), it is used to detect whether the thread is interrupted
- isInterrupted() is used to detect whether the thread is interrupted, return the interrupt flag bit, and call with the thread object
- interrupted() is used to detect whether the current Thread is interrupted, return and clear the interrupt flag bit. It is a static method called through the Thread class name
Abandonment method
- stop() is used to terminate the thread
- suspend() is used to suspend (pause) threads
- resume() is used to resume the thread