public static native void sleep(long millis) throws InterruptedException;
public static void sleep(long millis, int nanos) throws InterruptedException;
1
2
sleep方法的作用:使当前线程休眠指定毫秒数,但该线程并不会释放锁(The thread does not lose ownership of any monitors.)。
This implementation uses a loop of {@code this.wait} calls conditioned on {@code this.isAlive}. As a thread terminates the {@code this.notifyAll} method is invoked.
翻译:
Interrupts this thread.
If this thread is blocked in an invocation of the wait()、join()、sleep()… methods, then its interrupt status will be cleared and it will receive an InterruptedException.
翻译如下:
Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. In other words, if this method were to be called twice in succession, the second call would return false (unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it).
翻译如下:
/**
* Tests whether this thread has been interrupted. The <i>interrupted
* status</i> of the thread is unaffected by this method.
*
* 测试这个线程是否被中断。线程的中断状态不会被该方法清除。
*/
public boolean isInterrupted() {
return isInterrupted(false);//参数为false表示:不清除线程中断状态
}
1
2
3
4
5
6
7
8
9
/**
* 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);
1
2
3
4
5
6
7
8
下面通过代码理解:
public class ThreadTest {
public static void main(String[] args) {
ThreadTest test = new ThreadTest();
MyThread myThread = test.new MyThread();