Thread.interrupt()

1 package sun.nio.ch;
2 
3 public abstract interface Interruptible
4 {
5   public abstract void interrupt(Thread paramThread);
6 }
 1     /**
    
      中断此线程。 2 * Interrupts this thread. 3 * 除非当前线程正在中断自身(这始终是允许的),否则将调用此线程的{@link checkaccess()checkaccess}方法,这可能会导致引发{@link securityexception}。 4 * <p> Unless the current thread is interrupting itself, which is 5 * always permitted, the {@link #checkAccess() checkAccess} method 6 * of this thread is invoked, which may cause a {@link 7 * SecurityException} to be thrown. 8 * 如果这个线程在调用{wait()}、{wait(long)}、或{join()}、{join(long)}、{join(long,int)}、{sleep(long)}、
或{sleep(long,int)}方法的{wait(long,int)}方法时被阻塞,那么它的中断状态将被清除,并接收{@link interruptedexception}。
9 * <p> If this thread is blocked in an invocation of the {@link 10 * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link 11 * Object#wait(long, int) wait(long, int)} methods of the {@link Object} 12 * class, or of the {@link #join()}, {@link #join(long)}, {@link 13 * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)}, 14 * methods of this class, then its interrupt status will be cleared and it 15 * will receive an {@link InterruptedException}. 16 * 如果此线程在{@link java.nio.channel s.interruptiblechannel interruptiblechannel}上的I/O操作中被阻塞,则通道将关闭,
线程的中断状态将被设置,线程将收到{@link java.nio.channels.closedbyInterruption}。
17 * <p> If this thread is blocked in an I/O operation upon an {@link 18 * java.nio.channels.InterruptibleChannel InterruptibleChannel} 19 * then the channel will be closed, the thread's interrupt 20 * status will be set, and the thread will receive a {@link 21 * java.nio.channels.ClosedByInterruptException}. 22 * 如果这个线程在{@link java.nio.channels.selector}中被阻塞,那么该线程的中断状态将被设置,并且它将立即从选择操作返回,可能具有非零值,
      就像调用了选择器的{@link java.nio.channels.selector{wakeup-wakeup}方法一样。
23 * <p> If this thread is blocked in a {@link java.nio.channels.Selector} 24 * then the thread's interrupt status will be set and it will return 25 * immediately from the selection operation, possibly with a non-zero 26 * value, just as if the selector's {@link 27 * java.nio.channels.Selector#wakeup wakeup} method were invoked. 28 * 如果前面的条件都不成立,那么这个线程的中断状态将被设置。 29 * <p> If none of the previous conditions hold then this thread's interrupt 30 * status will be set. </p> 31 * 中断不活动的线程不必有任何效果。 32 * <p> Interrupting a thread that is not alive need not have any effect. 33 * 34 * @throws SecurityException 35 * if the current thread cannot modify this thread 36 * 37 * @revised 6.0 38 * @spec JSR-51 39 */ 40 public void interrupt() { 41 if (this != Thread.currentThread()) 42 checkAccess(); 43 44 synchronized (blockerLock) { 45 Interruptible b = blocker; 46 if (b != null) { 47 interrupt0(); // Just to set the interrupt flag 48 b.interrupt(this); 49 return; 50 } 51 } 52 interrupt0(); 53 }
 1 void os::interrupt(Thread* thread) {
 2 
 3   assert(Thread::current() == thread || Threads_lock->owned_by_self(),
 4 
 5     "possibility of dangling Thread pointer");
 6 
 7 
 8   OSThread* osthread = thread->osthread();
 9 
10 
11   if (!osthread->interrupted()) {
12     设置中断标识
13     osthread->set_interrupted(true);
14 
15     // More than one thread can get here with the same value of osthread,
16 
17     // resulting in multiple notifications.  We do, however, want the store
18 
19     // to interrupted() to be visible to other threads before we execute unpark().
20 
21     OrderAccess::fence();
22 
23     ParkEvent * const slp = thread->_SleepEvent ;
24 
25     if (slp != NULL) slp->unpark() ;
26 
27   }
28 
29 
30   // For JSR166. Unpark even if interrupt status already was set
31 
32   if (thread->is_Java_thread())
33 
34     ((JavaThread*)thread)->parker()->unpark();
35 
36 
37   ParkEvent * ev = thread->_ParkEvent ;
38 
39   if (ev != NULL) ev->unpark() ;
40 
41 
42 }
 1 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
 2 
 3   JVMWrapper("JVM_Interrupt");
 4 
 5 
 6   // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
 7 
 8   oop java_thread = JNIHandles::resolve_non_null(jthread);
 9 
10   MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
11 
12   // We need to re-resolve the java_thread, since a GC might have happened during the
13 
14   // acquire of the lock
15 
16   JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
17 
18   if (thr != NULL) {
19 
20     Thread::interrupt(thr);
21 
22   }
23 
24 JVM_END
总结:interrupt()方法就是给线程设置中断标志

猜你喜欢

转载自www.cnblogs.com/xiaofan156/p/11782229.html