Copy the code code as follows:
// Start thread
public void start( );
public void run( );
// Suspend and wake up threads
public void resume( ); // Not recommended for use
public void suspend( ); // Not recommended for use
public static void sleep(long millis);
public static void sleep(long millis, int nanos);
// Terminate thread
public void stop( ); // Not recommended for use
public void interrupt( );
// Get thread status
public boolean isAlive( );
public boolean isInterrupted( );
public static boolean interrupted( );
// join method
public void join( ) throws InterruptedException;
1. Create and run threads
The thread does not execute the code in the run method immediately after it is established, but is in a waiting state. When the thread is in the waiting state, you can set various attributes of the thread through the methods of the Thread class, such as the thread's priority (setPriority), thread name (setName), and thread type (setDaemon).
When the start method is called, the thread starts executing the code in the run method. The thread enters the running state. You can use the isAlive method of the Thread class to determine whether the thread is running. When the thread is in the running state, isAlive returns true. When isAlive returns false, the thread may be in the waiting state or in the stopped state. The following code demonstrates the switching between the three states of thread creation, running and stopping, and outputs the corresponding isAlive return value.
Copy the code code as follows:
package chapter2;
public class LifeCycle extends Thread
{
public void run()
{
int n = 0;
while ((++n) < 1000);
}
public static void main(String[] args) throws Exception
{
LifeCycle thread1 = new LifeCycle();
System.out.println("isAlive: " + thread1.isAlive());
thread1.start();
System.out.println("isAlive: " + thread1.isAlive());
thread1.join(); // Wait for thread thread1 to end before continuing execution
System.out.println("thread1 has ended!");
System.out.println("isAlive: " + thread1.isAlive());
}
}
Please note that the join method is used in the above code. The main function of this method is to ensure that the program continues to run after the thread's run method is completed. This method will be introduced in a later article.
The result of running the above code:
isAlive: false
isAlive: true
thread1 has ended!
isAlive: false
2. Suspend and wake up threads
Once the thread starts executing the run method, it will not exit until the run method is completed. However, during the execution of the thread, you can temporarily stop the thread execution through two methods. These two methods are suspend and sleep. After using suspend to suspend a thread, you can wake it up through the resume method. After using sleep to make the thread sleep, the thread can only be in the ready state after the set time (after the thread sleep ends, the thread may not execute immediately, but only enters the ready state, waiting for the system to schedule).
Although suspend and resume can easily suspend and wake up threads, using these two methods may cause some unpredictable things to happen. Therefore, these two methods are marked as deprecated (protest), which indicates that in These two methods may be deleted in future jdk versions, so try not to use these two methods to operate threads. The following code demonstrates the use of sleep, suspend and resume methods.
Copy the code code as follows:
package chapter2;
public class MyThread extends Thread
{
class SleepThread extends Thread
{
public void run()
{
try
{
sleep(2000);
}
catch (Exception e)
{
}
}
}
public void run()
{
while(true)
System.out.println(new java.util.Date().getTime());
}
public static void main(String[] args) throws Exception
{
MyThread thread = new MyThread();
SleepThread sleepThread = thread.new SleepThread();
sleepThread.start(); // Start running thread sleepThread
sleepThread.join(); // Delay thread sleepThread for 2 seconds
thread.start();
boolean flag = false;
while(true)
{
sleep(5000); // Delay the main thread for 5 seconds
flag = !flag;
if (flag)
thread.suspend();
else
thread.resume();
}
}
}
On the surface, the effects of using sleep and suspend are similar, but the sleep method is not equivalent to suspend. The biggest difference between them is that you can suspend another thread in one thread through the suspend method. For example, in the above code, the thread thread is suspended in the main thread. Sleep only works on the currently executing thread. In the above code, sleepThread and the main thread sleep for 2 seconds and 5 seconds respectively. When using sleep, be aware that you cannot sleep one thread in another thread. For example, using the thread.sleep(2000) method in the main method cannot make the thread thread sleep for 2 seconds, but can only make the main thread sleep for 2 seconds.
There are two points to note when using the sleep method:
1. The sleep method has two overloaded forms. One of the overloaded forms can not only set milliseconds, but also nanoseconds (1,000,000 nanoseconds is equal to 1 millisecond). However, the Java virtual machine on most operating system platforms is not accurate to nanoseconds. Therefore, if nanoseconds are set for sleep, the Java virtual machine will take the millisecond closest to this value.
2. Throws or try{...}catch{...} must be used when using the sleep method. Because the run method cannot use throws, you can only use try{...}catch{...}. When the thread is sleeping and the interrupt method (this method will be discussed in 2.3.3) is used to interrupt the thread, sleep will throw an InterruptedException. The sleep method is defined as follows:
Copy the code code as follows:
public static void sleep(long millis) throws InterruptedException
public static void sleep(long millis, int nanos) throws InterruptedException
3. Three ways to terminate a thread
There are three ways to terminate a thread.
1. Use the exit flag to make the thread exit normally, that is, the thread terminates when the run method is completed.
2. Use the stop method to forcefully terminate the thread (this method is not recommended because stop, like suspend and resume, may also produce unpredictable results).
3. Use the interrupt method to interrupt the thread.
1. Terminate the thread using the exit flag
When the run method is executed, the thread will exit. But sometimes the run method never ends. For example, threads are used in server programs to monitor client requests, or other tasks that require cyclic processing. In this case, these tasks are usually placed in a loop, such as a while loop. If you want the loop to run forever, you can use while(true){...} to handle it. But if you want to make the while loop exit under certain conditions, the most direct way is to set a boolean type flag, and set this flag to true or false to control whether the while loop exits. An example of terminating a thread using the exit flag is given below.
Copy the code code as follows:
package chapter2;
public class ThreadFlag extends Thread
{
public volatile boolean exit = false;
public void run()
{
while (!exit);
}
public static void main(String[] args) throws Exception
{
ThreadFlag thread = new ThreadFlag();
thread.start();
sleep(5000); //The main thread delays for 5 seconds
thread.exit = true; // Terminate thread thread
thread.join();
System.out.println("Thread exited!");
}
}
An exit flag, exit, is defined in the above code. When exit is true, the while loop exits, and the default value of exit is false. When defining exit, a Java keyword volatile is used. The purpose of this keyword is to synchronize exit, which means that only one thread can modify the value of exit at the same time.
2. Use the stop method to terminate the thread
Use the stop method to forcefully terminate a running or suspended thread. We can use the following code to terminate the thread:
thread.stop();
Although using the above code can terminate the thread, using the stop method is very dangerous. Just like turning off the computer power suddenly instead of shutting down according to the normal procedure, it may produce unpredictable results. Therefore, it is not recommended to use the stop method. Terminate the thread.
3. Use the interrupt method to terminate the thread
Using the interrupt method to terminate a thread can be divided into two situations:
(1) The thread is in a blocked state, such as using the sleep method.
(2) Use while(!isInterrupted()){...} to determine whether the thread is interrupted.
In the first case using the interrupt method, the sleep method will throw an InterruptedException exception, while in the second case the thread will exit directly. The code below demonstrates the use of the interrupt method in the first case.
Copy the code code as follows:
package chapter2;
public class ThreadInterrupt extends Thread
{
public void run()
{
try
{
sleep(50000); // delay 50 seconds
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
public static void main(String[] args) throws Exception
{
Thread thread = new ThreadInterrupt();
thread.start();
System.out.println("Press any key within 50 seconds to interrupt the thread!");
System.in.read();
thread.interrupt();
thread.join();
System.out.println("The thread has exited!");
}
}
The results of running the above code are as follows:
Copy the code code as follows:
Press any key within 50 seconds to interrupt the thread!
sleep interrupted
The thread has exited!
After calling the interrupt method, the sleep method throws an exception and then outputs the error message: sleep interrupted.
Note: There are two methods in the Thread class to determine whether the thread is terminated through the interrupt method. One is the static method interrupted(), and the other is the non-static method isInterrupted(). The difference between these two methods is that interrupted is used to determine whether the current thread is interrupted, while isInterrupted can be used to determine whether other threads are interrupted. Therefore, while (!isInterrupted()) can also be replaced by while (!Thread.interrupted()).