Interrupt () While one thread is running, another thread can interrupt it by invoking its corresponding Thread object's interrupt () method: public void interrupt () this method simply sets a flag in the destination thread indicating that it has been interrupted and returns right away. it is possible that a SecurityException will be thrown by interrupt (), indicating that the thread requesting the interrupt does no T have permission to interrupt the other thread. the security check is done by invoking the checkAccess () method on Thread, which in turn checks whether a SecurityManager has been installed, and if so, invokes its checkAccess (Thread) method. an in-depth authentication of security in Java is beyond the scope of this book, but you shoshould note that some methods of Thread and ThreadGroup cocould throw Secu RityExceptions. This method modifies the thread flag to indicate that the thread has been interrupted and will be returned immediately. In some environments with high security requirements (applets), SecuityManager may throw a SecurityException. IsInterrupted () You can check the interrupted status of any thread by invoking the isInterrupted () method on the Thread object: public boolean isInterrupted () This does not alter the status, but simply returns true if the thread has been interrupted and its interrupted flag has not yet been cleared. check whether the thread is interrupted. After the function is executed, the interrupt flag is not cleared. Thread. interrupted () You can use the Thread. interrupted () method to check (and implicitly reset to false) the interrupted status of the current thread: public static boolean interrupted () Because it is static, it cannot be invoked on a participant thread, but can only report the interrupted status of the thread that invoked it. this method returns true if the thread has been interrupted and its interrupted flag has not yet been cleared. unlike isInterrupted (), it automatically resets the interrupted flag to false. invoking Thread. interrupted () a second time wowould always return false unless the thread was reinterrupted. listing 5.4 shows an example of how you can use Thread. interrupted (). Check whether the thread is interrupted. After the program is executed, the interrupt mark is reset to false. Deprecated Methods suspend () and resume ()The Thread API contains two DeprecatedMethods that are used to temporarily stop and later restart a thread: public final void suspend () public final void resume () methods and classes are deprecated by Sun Microsystems to indicate that developers shoshould avoid using them. A deprecated method can still be used, but when the code is compiled, a warning is issued. deprecation is used to indicate a method or class is obsolete or dangerous and may be removed from future releases of the JDK. although you may still use deprecated methods, you shoshould use them sparingly and only when absolutely necessary. suspend () suspension thread resume () Restoration thread execution @ Deprecation indicates that this method is not recommended and may be removed from The suspend () in future jdk versions () method is deprecated as of JDK 1.2 because if a thread is suincluded at an inopportune time-such as when it is holding a lock for a shared resource-a deadlock condition may result. I explain and demonstrate deadlocks in Chapter 7, "Concurrent Access to Objects and Variables, "but let it suffice to say for now that a deadlock is a very bad thing and can cause a program to freeze up on a user. even when locks are not involved, a thread might be suincluded while in the middle of a long procedure that really shocould not be left in a partially completed state. the resume () method is deprecated because without any use of suspend (), it is not needed. suspend () is not recommended because: 1. When a thread occupies some shared resources, it is suspended, which can easily lead to a deadlock, 2. When you execute a large operation that cannot be partially completed, the thread may be suspended and resume () is not recommended because suspend () is not recommended () Deprecated Method stop () At times, one of the additional threads spawned in a multithreaded program is no longer needed, but it continues to execute statements-perhaps in an infinite loop. the Thread API calls des a stop () method to abruptly terminate a thread's execution: public final void stop () When this method is invoked on a thread, the thread immediately throws a java. lang. threadDeath error (a subclass of java. lang. err Or, which itself is a subclass of java. lang. throwable ). this exception propagates up the method call stack, executing any finally clses present and releasing any locks that are currently held. ultimately, this exception causes run () to abruptly return, and the thread dies. when the stop () method is called, the thread immediately throws ThreadDeath error (java. lang. the subclass of Error, java. lang. the Error itself is java. lang. the sub-class of Throwable), the thread will release all locks, run () will return immediately, the thread will die Th E stop () method is deprecated as of JDK 1.2 because it can lead to initialize upted data in objects. one problem is that when a thread is stopped abruptly, there is little opportunity to perform any cleanup work. another problem is that when stop () is invoked on a thread, the thread releases all the locks it is currently holding. the locks are definitely being held for a good reason-probably to keep othe R threads from accessing data elements that are not yet in a consistent state. this sudden release of the locks may leave some data in some objects in an inconsistent state with no warning about the possible failed uption. in aggregate cases, there are other ways to have a thread return from run () in an orderly manner that leaves all objects in a consistent state. reason for stop cancellation: 1. When a thread is roughly aborted, it may not be cleared. 2. Lock the machine. To prevent access to common resources, other threads are in a non-continuous state. After a thread is stopped, it will release all its locks, causing other threads to be discontinuous. An Alternative to stop (): work und: AlternateStop. java source code/** Created on 2005-7-8 ** Java Thread Programming-Paul Hyde * Copyright? 1999 Sams Publishing * Jonathan Q. bo Study Notes **/package org. tju. msnrl. jonathan. thread. chapter1;/*** @ author Jonathan Q. bo from tju msnrl ** E-mail: jonathan.q.bo@gmail.com * Blog: blog.csdn.net/jonathan_q_bo * blog.yesky.net/jonathanundersun ** Enjoy Life with Sun! **/Public class AlternateStop implements Runnable {private Thread runThread; Private volatile boolean stopRequest;/** @ See java. lang. runnable # run () */public void run () {this. runThread = Thread. currentThread (); stopRequest = false; int counter = 0; While (! StopRequest ){System. out. println ("running ...... count = "+ counter); counter ++; try {thread. sleep (300);} catch (interruptedexception e) {thread. currentthread (). interrupt () ;}} public void stopthread (){ This. stopRequest = true;If (this. runThread! = Null) {this. runThread. interrupt () ;}} public static void main (String [] args) {AlternateStop at = new AlternateStop (); Thread newThread = new Thread (at); newThread. start (); try {Thread. sleep (2000);} catch (InterruptedException e) {System. out. println (e. getMessage ();}. stopThread ();}} Daemon ThreadsThreads that are marked as daemons stop in a whole new way. daemon threads are used for background supporting tasks and are only needed while normal, nondaemon threads are still running. when the VM detects that the only remaining threads are daemon threads, it exits. if any NonDaemon thread is still alive, the VM will not exit. daemon threads provide a nice way of managing some sort of behind-the-scenes processing that is only necessary to support other nondaemon threads. daemon thread: it is executed in the background when other common non-daemon threads run. When other threads end and the rest are daemon threads, the virtual machine stops the daemon thread.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service