Method for stopping thread execution in Java, Java thread execution Method
The method used to stop thread execution in Java is chszs, which must be noted for reprinting. Blog homepage: http://blog.csdn.net/chszsone-Stop Theory
In Java programming, there are several methods to pause or stop the currently running thread. Thread. Sleep () is the most correct method for transferring a Thread to sleep. Some may ask why wait for wait () or notify ()? You must know that waiting or notification is not a good method.
The thread can be blocked by waiting for wait (). This is a method of conditional waiting. When the condition is met, the thread changes from blocking to waiting. Although a timeout setting can be placed while waiting for the wait () condition, the purpose of waiting for wait () is not the same, and waiting for wait () is designed for Java thread communication.
The sleep () method allows the thread to sleep at the specified time from the current start. Do not use sleep () instead of waiting for wait () or notifying notify (), or vice versa.
Waiting for wait () or notifying notify () should not be used to pause the thread. Another reason is that waiting for wait () or notifying notify () requires a lock. They can only be called from a synchronous method or a synchronized code block. The overhead of obtaining and Releasing locks is relatively large. In addition, the lock mechanism is not required if the thread is paused.
Sleep () is different from wait (). sleep () transfers the current thread to the waiting state and does not release any lock it holds () when the thread is transferred to the blocking status, the lock held by the thread will be released.
In short, Java multi-threaded programming is not simple. Even simple tasks, such as creating a thread, stopping a thread, or pausing a thread, must master Java APIs carefully.
Ii. practice of suspending or stopping a thread
In the following example, to pause a Thread, you can use the Thread. sleep () or TimeUnit. sleep () method. In this example, there are two threads, the main thread is started by JVM, And it executes the main () method. The second thread is T1, which is created by the main thread to run the game cyclically. The Runnable task we passed is an infinite loop that will run until we stop it. Note that I use the volatile keyword.
The main thread first starts thread T1 and then stops the thread using the stop () method.
In this example, we have two methods to stop the running of a Thread: Thread. sleep () or TimeUnit. sleep. The TimeUnit class can specify the second (TimeUnit. SECONDS) and the millisecond (TimeUnit. MILLISECONDS. In general, using the sleep () method of TimeUnit makes the code easier to read.
Import static java. lang. thread. currentThread; import java. util. concurrent. timeUnit; public class ThreadPauseDemo {public static void main (String args []) throws InterruptedException {Game game = new Game (); Thread t1 = new Thread (game, "T1 "); t1.start (); // stop the Game thread System. out. println (currentThread (). getName () + "is stopping game thread"); game. stop (); // view the status of the stopped Game thread TimeUnit. MILLISECONDS. sleep (200); System. out. println (currentThread (). getName () + "is finished now") ;}} class Game implements Runnable {private volatile boolean isStopped = false; public void run () {while (! IsStopped) {System. out. println ("Game thread is running ...... "); System. out. println ("Game thread is now going to pause"); try {Thread. sleep (200);} catch (InterruptedException e) {e. printStackTrace ();} System. out. println ("Game thread is now resumed ...... ");} System. out. println ("Game thread is stopped ...... ") ;}public void stop () {isStopped = true ;}}
The program output is as follows:
Game thread is running......main is stopping game threadGame thread is now going to pauseGame thread is now resumed......Game thread is stopped......main is finished now
Note:
Volatile Keyword: When a shared variable is modified by volatile, it ensures that the modified value is immediately updated to the primary storage. When other threads need to read this value, it reads the new value from the primary memory. The volatile keyword ensures visibility. The visibility of common shared variables cannot be guaranteed, because after the common shared variables are modified, it is not clear when they are written to the primary storage. When other threads read the shared variables, in this case, the memory may still be the old value, so visibility cannot be guaranteed.
Synchronized and Lock can also ensure visibility. synchronized and Lock can ensure that only one thread obtains the Lock at the same time and then executes the synchronization code, before the lock is released, the modification to the variable is refreshed to the primary storage. This ensures visibility.
After being modified by volatile, a shared variable (the member variable of the class and the static member variable of the class) has two meanings:
The volatile keyword disables Command Re-sorting. It has two meanings:
Iii. Summary of sleep () method
The Thread. sleep () method can pause or stop a Thread. Note the following details:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.