1,Sleep(long)让出cpu的使用权,不释放锁 Wait(long)让出cpu使用权,并释放锁。
2,Join()是通过wait函数实现的主线程阻塞。
3,interrupted()函数是Thread静态函数,用来检测当前线程的interrupt状态,检测完成后,状态清空
4,yield()函数告诉cpu我可以让出使用权了,同优先级的线程可以使用了,并不一定立即让出,让出后立即加入到cpu抢夺中。
5,interrupt()函数可以打断线程的wait和sleep的等待,但是不能够打断等待锁资源而阻塞的线程。
6,stop()已经废弃,停止线程过去暴力,不能实现软着陆,会丢失数据。
线程间状态转换的图。
Sleep(long)让出cpu的使用权,不释放锁 Wait(long)让出cpu使用权,并释放锁。
public class TestThread {
final static Object syn = new Object();
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Thread(){
public void run() {
System.out.println(getName()+"等待获取锁。。。。。。。。。。。");
synchronized (syn) {
System.out.println(getName()+"获取到锁。。。。。。。。。。。");
System.out.println(getName()+"进入睡眠状态。。。。。。。。。。。");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(getName()+"睡眠完毕。。。。。。。。。。。");
}
}
}.start();
new Thread(){
public void run() {
System.out.println(getName()+"等待获取锁。。。。。。。。。。。");
long time = System.currentTimeMillis();
synchronized (syn) {
System.out.println(getName()+"获取到锁,已过去"+(System.currentTimeMillis()-time)+"。。。。。。。。。。。");
System.out.println(getName()+"Do Somethings。。。。。。。。。。。");
}
}
}.start();
}
}
输出:
Thread-0等待获取锁。。。。。。。。。。。
Thread-0获取到锁。。。。。。。。。。。
Thread-0进入睡眠状态。。。。。。。。。。。
Thread-1等待获取锁。。。。。。。。。。。
Thread-0睡眠完毕。。。。。。。。。。。
Thread-1获取到锁,已过去10001。。。。。。。。。。。
Thread-1Do Somethings。。。。。。。。。。。
从输出上来看,最后一个thread的确等待了10秒钟才获取到锁。
Join()是通过wait函数实现的主线程阻塞。
join()函数源码
public final void join() throws InterruptedException {
join(0);
}
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;
}
}
}
很简单的一块代码,通过判断线程是否是alive状态,如果不是一直调用wait函数阻塞主线程,join函数是synchronized修饰的。
interrupted()函数是Thread静态函数,用来检测当前线程的interrupt状态,检测完成后,状态清空
interrupted源码,通过下面源码我们能够知道,函数班判断的当前线程,而且在调用一个native方法private native boolean isInterrupted(boolean ClearInterrupted)
通过函数的注释能够知道,用来测试线程是否已经中断,参数用来决定是否重置中断标志。
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
public boolean isInterrupted() {
return isInterrupted(false);
}
/**
* 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);
yield()函数告诉cpu我可以让出使用权了,同优先级的线程可以使用了,并不一定立即让出,让出后立即加入到cpu抢夺中。
yield()是一个native方法,
/**
* Causes the currently executing thread object to temporarily pause
* and allow other threads to execute.
*/
public static native void yield();
new Thread(){
public void run() {
System.out.println(getName()+"等待获取锁。。。。。。。。。。。");
synchronized (syn) {
System.out.println(getName()+"获取到锁。。。。。。。。。。。");
System.out.println(getName()+"我可以让出cpu了。。。。。。。。。。。");
yield();
System.out.println(getName()+"我还在执行。。。。。。。。。。。");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
new Thread(){
public void run() {
System.out.println(getName()+"等待获取锁。。。。。。。。。。。");
long time = System.currentTimeMillis();
synchronized (syn) {
System.out.println(getName()+"获取到锁,已过去"+(System.currentTimeMillis()-time)+"。。。。。。。。。。。");
System.out.println(getName()+"Do Somethings。。。。。。。。。。。");
}
}
}.start();
输出
Thread-0等待获取锁。。。。。。。。。。。
Thread-0获取到锁。。。。。。。。。。。
Thread-0我可以让出cpu了。。。。。。。。。。。
Thread-0我还在执行。。。。。。。。。。。
Thread-1等待获取锁。。。。。。。。。。。
Thread-1获取到锁,已过去2001。。。。。。。。。。。
Thread-1Do Somethings。。。。。。。。。。。
从上面看出,yeild并不会让出锁,也可能不会立即让出cpu。
interrupt()函数可以打断线程的wait和sleep的等待,但是不能够打断等待锁资源而阻塞的线程。
new Thread(){
public void run() {
System.out.println(getName()+"等待获取锁。。。。。。。。。。。");
synchronized (syn) {
System.out.println(getName()+"获取到锁。。。。。。。。。。。");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
Thread thread = new Thread(){
public void run() {
System.out.println(getName()+"等待获取锁。。。。。。。。。。。");
long time = System.currentTimeMillis();
synchronized (syn) {
System.out.println(getName()+"获取到锁,已过去"+(System.currentTimeMillis()-time)+"。。。。。。。。。。。");
System.out.println(getName()+"Do Somethings。。。。。。。。。。。");
}
}
};
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("主线程要打断等待锁线程。。。。。。。。。。。");
thread.interrupt();
输出:
Thread-0等待获取锁。。。。。。。。。。。
Thread-1等待获取锁。。。。。。。。。。。
Thread-0获取到锁。。。。。。。。。。。
主线程要打断等待锁线程。。。。。。。。。。。
Thread-1获取到锁,已过去10001。。。。。。。。。。。
Thread-1Do Somethings。。。。。。。。。。。
我们看到即使我们调用了interrupt,Thread-1任然还在等待获取锁,一直到Thread-0释放之后才获取到。晚上好多人说,interrupt可以中断一个阻塞的线程,通过jstack命令,我们获取到了thread-1的状态。
interrupt并没有打断一个等待锁资源的线程。