调试demo
public class MyThread extends Thread {
@Override
public void run() {
for (int i=0;i<50000;i++){
System.out.println("i==========="+i);
}
}
public static void main(String[] args){
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(2000);
myThread.interrupt();
System.out.println("该线程是否停止1?+"+myThread.interrupted());
System.out.println("该线程是否停止2?+"+myThread.interrupted());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
导致此线程开始执行;
Java虚拟机调用此线程的<code> run 方法。 结果是两个线程同时运行:当前线程(从调用返回到 start 方法)和另一个线程(执行其运行方法)。 start方法只能调用一次
public synchronized void start() {
if (threadStatus != 0)
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
调用start方法
/** * Adds the specified thread to this thread group. * * <p> Note: This method is called from both library code * and the Virtual Machine. It is called from VM to add * certain system threads to the system thread group. * * @param t * the Thread to be added * * @throws IllegalThreadStateException * if the Thread group has been destroyed */
从库代码和虚拟机调用此方法。从VM调用它以将某些系统线程添加到系统线程组。
void add(Thread t) {
synchronized (this) {
if (destroyed) {
throw new IllegalThreadStateException();
}
if (threads == null) {
threads = new Thread[4];
} else if (nthreads == threads.length) {
threads = Arrays.copyOf(threads, nthreads * 2);
}
threads[nthreads] = t;
// This is done last so it doesn't matter in case the
// thread is killed
nthreads++;
// The thread is now a fully fledged member of the group, even
// though it may, or may not, have been started yet. It will prevent
// the group from being destroyed so the unstarted Threads count is
// decremented.
nUnstartedThreads--;
}
}
中断该线程,除非当前线程中断其自己,这是被允许的,当checkAccess方法被调用,可能会抛出安全异常SecurityException
如果该线程调用Object wait()方法或者wait(long)或者wait(long,int)
或者join(),join(long),join(long,int),sleep(long),sleep(long,int)
此类的方法,然后它的中断状态将被清除,它将*收到{@link InterruptedException}。
如果在{@link * java.nio.channels.InterruptibleChannel InterruptibleChannel} 的I / O操作中阻止此线程,则通道将关闭,线程的中断状态将被设置,并且线程将收到{@link * java.nio.channels.ClosedByInterruptException}。
如果此线程在{@link java.nio.channels.Selector} 中被阻止,则线程的中断状态将被设置,它将立即从选择操作返回,可能是非零值,就像调用了选择器的{@link * java.nio.channels.Selector #wakeup wakeup}方法一样。
如果以前的条件都不成立,那么将设置该线程的中断状态。
中断非活动的线程无需任何效果。
如果当前线程无法修改此线程* @throws SecurityException
public void interrupt() {
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();
}
确定当前运行的线程是否具有修改此线程的权限。
如果有安全管理器,则此线程作为参数调用其checkAccess 方法。这可能导致抛出 SecurityException
* Gets the system security interface. * * @return if a security manager has already been established for the * current application, then that security manager is returned; * otherwise, <code>null</code> is returned. * @see #setSecurityManager */
获取系统安全性界面。
@return如果已经为当前应用程序建立了安全管理器,则返回该安全管理器;
否则,返回 null
public final void checkAccess() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkAccess(this);
}
}
如果调用线程不允许修改线程参数。
通过 stop , suspend ,resume , setPriority <为当前安全管理器调用此方法 setName ,setDaemon Thread类的方法。
如果线程参数是系统线程(属于*带有null 父线程的线程组),则此方法使用*<代码调用 checkPermission </ code > RuntimePermission(“modifyThread”)权限。
如果线程参数不是系统线程,此方法只默默返回。
应用需要更严格策略的程序应覆盖此方法。如果覆盖此方法,则覆盖*的方法应另外检查调用线程是否具有 RuntimePermission(“modifyThread”)权限,如果是,则以默认方式返回。这是为了确保允许代码授予权限(例如JDK本身)操纵任何线程。
如果重写此方法,则super.checkAccess 应由重写方法中的第一个语句调用,或者*等效安全检查应放在重写方法中。
public void checkAccess(Thread t) {
if (t == null) {
throw new NullPointerException("thread can't be null");
}
if (t.getThreadGroup() == rootGroup) {
checkPermission(SecurityConstants.MODIFY_THREAD_PERMISSION);
} else {
// just return
}
}
为空不进行检查
之后打印输出
整个过程的整理