Home > Java > javaTutorial > body text

The use of the thread life cycle of java threads

黄舟
Release: 2016-12-19 14:37:14
Original
1346 people have browsed it

Just like people are born, old, sick and die, threads also have to go through four different states: starting (waiting), running, hanging and stopping. These four states can be controlled through methods in the Thread class. The methods related to these four states in the Thread class are given below.

// 开始线程
     public void start( );
     public void run( );

     // 挂起和唤醒线程
     public void resume( );     // 不建议使用
     public void suspend( );    // 不建议使用
     public static void sleep(long millis);
     public static void sleep(long millis, int nanos);

     // 终止线程
     public void stop( );       // 不建议使用
     public void interrupt( );

     // 得到线程状态
     public boolean isAlive( );
     public boolean isInterrupted( );
     public static boolean interrupted( );

     // join方法
     public void join( ) throws InterruptedException;
Copy after login

1. Create and run the thread


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.

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();  // 等线程thread1结束后再继续执行 
         System.out.println("thread1已经结束!");
         System.out.println("isAlive: " + thread1.isAlive());
     }
 }
Copy after login

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


above The result of running the code:

isAlive: false
isAlive: true
thread1已经结束!
isAlive: false
Copy after login

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, there are two methods that can be used to temporarily stop the thread execution. These two methods are suspend and sleep. After using suspend to suspend the thread, you can wake up the thread 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.

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(); // 开始运行线程sleepThread
         sleepThread.join();  // 使线程sleepThread延迟2秒
         thread.start();
         boolean flag = false;
         while (true)
         {
             sleep(5000);  // 使主线程延迟5秒
             flag = !flag;
             if (flag)
                 thread.suspend(); 
             else
                 thread.resume();
         }
     }
 }
Copy after login

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 use the suspend method in one thread to suspend another thread. As 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 which 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. When using the sleep method, you must use throws or try{…}catch{…}. Because the run method cannot use throws, you can only use try{…}catch{…}. When the thread is sleeping, Sleep will throw an InterruptedException when interrupting a thread using the interrupt method (this method will be discussed in 2.3.3). The sleep method is defined as follows:

1 public static void sleep(long millis)  throws InterruptedException
2 public static void sleep(long millis,  int nanos)  throws InterruptedException
Copy after login

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 forcibly 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. Use the exit flag to terminate the thread

当run方法执行完后,线程就会退出。但有时run方法是永远不会结束的。如在服务端程序中使用线程进行监听客户端请求,或是其他的需要循环处理的任务。在这种情况下,一般是将这些任务放在一个循环中,如while循环。如果想让循环永远运行下去,可以使用while(true){……}来处理。但要想使while循环在某一特定条件下退出,最直接的方法就是设一个boolean类型的标志,并通过设置这个标志为true或false来控制while循环是否退出。下面给出了一个利用退出标志终止线程的例子。

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); // 主线程延迟5秒
         thread.exit = true;  // 终止线程thread
         thread.join();
         System.out.println("线程退出!");
     }
 }
Copy after login

在上面代码中定义了一个退出标志exit,当exit为true时,while循环退出,exit的默认值为false.在定义exit时,使用了一个Java关键字volatile,这个关键字的目的是使exit同步,也就是说在同一时刻只能由一个线程来修改exit的值,


2. 使用stop方法终止线程

使用stop方法可以强行终止正在运行或挂起的线程。我们可以使用如下的代码来终止线程:

1 thread.stop();
Copy after login

虽然使用上面的代码可以终止线程,但使用stop方法是很危险的,就象突然关闭计算机电源,而不是按正常程序关机一样,可能会产生不可预料的结果,因此,并不推荐使用stop方法来终止线程。


3. 使用interrupt方法终止线程

使用interrupt方法来终端线程可分为两种情况:

(1)线程处于阻塞状态,如使用了sleep方法。

(2)使用while(!isInterrupted()){……}来判断线程是否被中断。

在第一种情况下使用interrupt方法,sleep方法将抛出一个InterruptedException例外,而在第二种情况下线程将直接退出。下面的代码演示了在第一种情况下使用interrupt方法。

package chapter2;

 public class ThreadInterrupt extends Thread
 {
     public void run()
     {
         try
         {
             sleep(50000);  // 延迟50秒
         }
         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("在50秒之内按任意键中断线程!");
         System.in.read();
         thread.interrupt();
         thread.join();
         System.out.println("线程已经退出!");
     }
 }
Copy after login

上面代码的运行结果如下:

在50秒之内按任意键中断线程!
 sleep interrupted
 线程已经退出!
Copy after login

在调用interrupt方法后, sleep方法抛出异常,然后输出错误信息:sleep interrupted.


    注意:在Thread类中有两个方法可以判断线程是否通过interrupt方法被终止。一个是静态的方法interrupted(),一个是非静态的方法isInterrupted(),这两个方法的区别是interrupted用来判断当前线是否被中断,而isInterrupted可以用来判断其他线程是否被中断。因此,while (!isInterrupted())也可以换成while (!Thread.interrupted())。


 以上就是java线程之线程的生命周期的使用的内容,更多相关内容请关注PHP中文网(www.php.cn)! 


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!