/**
* Interrupts this thread.
* 中断这个线程
*
* <p> Unless the current thread is interrupting itself, which is
* always permitted, the {@link #checkAccess() checkAccess} method
* of this thread is invoked, which may cause a {@link
* SecurityException} to be thrown.
* 如果不是当前线程中断自身,这经常是被允许的。不过要检查安全性,
* 可能会抛出安全异常的。
*
* <p> If this thread is blocked in an invocation of the {@link
* Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
* Object#wait(long, int) wait(long, int)} methods of the {@link Object}
* class, or of the {@link #join()}, {@link #join(long)}, {@link
* #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
* methods of this class, then its interrupt status will be cleared and it
* will receive an {@link InterruptedException}.
* 如果中断的线程由于调用一个Object对象的多个wait方法或者当前对象的
* join,sleep方法而正处于阻塞状态(仅表示没有获得CPU的时间片执行,不表示
* 线程的BLOCKED状态)。那么它的中断状态将被清除(复位),而且它会收到中
* 断异常。
*
* <p> If this thread is blocked in an I/O operation upon an {@link
* java.nio.channels.InterruptibleChannel InterruptibleChannel}
* then the channel will be closed, the thread's interrupt
* status will be set, and the thread will receive a {@link
* java.nio.channels.ClosedByInterruptException}.
*
* <p> If this thread is blocked in a {@link java.nio.channels.Selector}
* then the thread's interrupt status will be set and it will return
* immediately from the selection operation, possibly with a non-zero
* value, just as if the selector's {@link
* java.nio.channels.Selector#wakeup wakeup} method were invoked.
*
* <p> If none of the previous conditions hold then this thread's interrupt
* status will be set. </p>
* 如果上述的条件都没有比中的话,那么这个线程的中断标志位将被设置。
*
* <p> Interrupting a thread that is not alive need not have any effect.
* 中断一个不存活的线程(未启动或已结束)不会有任何影响
*
* @throws SecurityException
* if the current thread cannot modify this thread
*
* @revised 6.0
* @spec JSR-51
*/
public void interrupt() {
//检查当前线程对this线程的安全权限,如果不允许修改,会抛出异常
if (this != Thread.currentThread())
checkAccess();
//加锁同步
synchronized (blockerLock) {
Interruptible b = blocker;
if (b != null) {
interrupt0(); // Just to set the interrupt flag
b.interrupt(this);
return;
}
}
interrupt0(); //设置标识位,本地方法
}
/***
* 此示例中10个线程在执行时,,需要等待前一个线程执行完;
* 比如线程0要等待main线程执行完
* 线程9要等到线程8执行完
*
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Thread previous = Thread.currentThread();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Dimon(previous), String.valueOf(i));
thread.start();
previous = thread;
}
TimeUnit.SECONDS.sleep(5);
System.out.println(Thread.currentThread().getName() + "terminate");
}
static class Dimon implements Runnable{
private Thread thread;
public Dimon(Thread thread){
this.thread = thread;
}
@Override
public void run() {
try {
thread.join();
}catch (Exception e){
}
System.out.println( Thread.currentThread().getName() + ":terminate" );
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有