thread方法总结
复习时遇见的小问题,老师给的课件没有详细地解释:在判断线程状态时用到了好几个方法:例如Thread.interrupt(),Thread.interrupted(),Thread.isInterrupted()等。他们之间又有什么区别?我稍微翻了一下java源码,在这里小小总结一下:
1.Thread.interrupt()
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方法检查权限。如果当前线程因被调用Object.wait(), 或者线程本身的join()方法,处于阻塞状态中,此时调用interrupt方法会使抛出InterruptedException,而且线程的阻塞状态将会被清除。
2.Thread.interrupted()
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
这个方法非常简洁,因为它只调用了currentThread类的isInterrupted方法。interrupted是一个静态方法,而且向isInterrupted传递了一个布尔量:true,这意味着调用了一次interrupted后该线程的状态将重置为不中断。
3.isinterrupted()
public boolean isInterrupted() {
return isInterrupted(false);
}
调用了isInterrupted(boolean ClearInterrupted)的有参方法:
/**
* Tests if some Thread has been interrupted. The interrupted state
* is reset or not based on the value of ClearInterrupted that is
* passed.
*/
private native boolean isInterrupted(boolean ClearInterrupted);
这个参数如果为true,则重置中断状态,否则将不会重置。由于isinterrupted传进来的是false,因此该线程的中断状态不会被改变。
由此可见,interrupted与isInterrupted最大的区别就是是否重置状态。
4.sleep()
public static void sleep(long millis, int nanos)
throws InterruptedException {
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
millis++;
}
sleep(millis);
}
使线程休眠,可以看到对休眠时间有限制。第一个参数为毫秒,第二个参数为纳秒。
5.yield()
/**
* A hint to the scheduler that the current thread is willing to yield
* its current use of a processor. The scheduler is free to ignore this
* hint.
*
* <p> Yield is a heuristic attempt to improve relative progression
* between threads that would otherwise over-utilise a CPU. Its use
* should be combined with detailed profiling and benchmarking to
* ensure that it actually has the desired effect.
*
* <p> It is rarely appropriate to use this method. It may be useful
* for debugging or testing purposes, where it may help to reproduce
* bugs due to race conditions. It may also be useful when designing
* concurrency control constructs such as the ones in the
* {@link java.util.concurrent.locks} package.
*/
public static native void yield();
向调度器提示当前线程愿意放弃它当前使用的处理器,调度器可以随意忽略这个提示。
6.join()
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
阅读源码可知,线程A调用线程B的join()方法,将会使A等待B执行,直到线程B终止。如果传入参数,将会使A等待B执行time的时间,如果time时间到达,将会切换进A线程,继续执行A线程。参数如果小于0,将会抛出一个IllegalArgumentException异常。