下面这张是Java线程状态改变的图例。
下面就其中几点和函数进行说明
运行态
Java将操作系统中的"运行中running"和"就绪ready"两种状态统称为运行态runnable。runing和ready的切换由操作系统进行调度。
join
让当前线程等待join线程执行完毕。如果join()方法在一个线程实例上调用,则当前运行着的线程将阻塞直到这个线程实例执行完毕。
该方法可以实现一个线程在另一个线程结束后再执行。
public class JoinTest {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println("子线程执行完毕");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程执行完毕");
}
}
执行结果如下:
子线程执行完毕
主线程执行完毕
main线程会等待thread执行完毕之后再执行。如果去掉thread.join()的结果如下:
主线程执行完毕
子线程执行完毕
yield
Thread.yield()
方法表示暂停当前正在执行的线程对象,并执行其他线程。
在多线程的情况下,由CPU决定执行哪一个线程,而yield()方法就是暂停当前的线程,让给其他线程(包括它自己)执行,具体由谁执行由CPU决定。
public class YieldTest {
public static void main(String[] args) {
YieldRunnable runnable1 = new YieldRunnable();
YieldRunnable runnable2 = new YieldRunnable();
Thread thread1 = new Thread(runnable1, "线程1");
Thread thread2 = new Thread(runnable2, "线程2");
thread1.start();
thread2.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runnable1.isRunning = false;
runnable2.isRunning = false;
}
static class YieldRunnable implements Runnable {
public volatile boolean isRunning = true;
@Override
public void run() {
while (isRunning){
for (int i=0; i< 10; i++){
System.out.println(Thread.currentThread().getName() + "执行");
Thread.yield();
}
}
}
}
}
上述代码的部分执行结果如下:
。。。。
线程1执行
线程2执行
线程1执行
线程2执行
线程1执行
线程2执行
线程1执行
线程2执行
线程1执行
线程2执行
线程1执行
线程1执行
线程1执行
线程1执行
。。。。
可以看到线程1和线程2的执行是没有规律的, 虽然执行了yield,让出了当前执行时间, 但是下一次由谁执行是不确定的, 完全是由操作系统自己决定。
总结yield
和join
的区别如下:
- yield英文为屈服,可以理解当前线程屈服于CPU的调度, 自己并没有真正的主导权, 只能被动等待CPU。
- join英文为加入,说明自己有主导权, 加入之后就是真正的等待,直到自己执行完成。
sleep
sleep只是单纯的睡觉,当前线程拥有的锁还牢牢握在自己手上。
wait
wait方法会让出对象锁, 并由其他线程notify之后竞争锁。
当A线程调用B对象的该方法时,首先A线程需要持有该对象(一般是利用synchronized加锁持有该对象B), 然后A线程会处于Waiting状态,此时加载对象B上面的锁也会被释放,直到有其他线程调用B对象的notify或者notifyAll方法,A线程才能继续执行wait()方法之后的代码。
注意一般该方法放到循环中去,因为有时候会出现wait被唤醒但condition仍为true的情况,此时需要重新调用wait让出锁继续等待。
notify
需要与wait()结合使用。
通知正在wait的线程可以开始竞争当前线程的锁了,线程调度器会随机选取一个正在wait的线程出来。若是调用notifyAll方法则所有正在wait的线程会同时竞争该锁。
注意当前线程notify之后并不会马上让出锁,而是执行完同步代码块之后才会让出锁。
线程中断
一个线程调用Thread.interrupt()会发生什么?
对于线程的中断应该是由线程自己来决定,而不能由其他线程决定,这也是Thread的stop等方法被抛弃的原因。所以当调用interrupt()方法时,**只是将这个线程的状态修改为中断状态,**至于该线程是否真要中断,完全取决于该线程自己的实现方式。
具体来说,当对一个线程调用interrupt方法时,有如下几种情形:
- 当该线程处于阻塞等待状态(如调用sleep,wait等方法)时,此时该线程会立即退出阻塞等待状态,并抛出一个InterruptException异常,仅此而已
- 当该线程处于运行状态,此时会将该线程的中断标志设置为true,仅此而已。该线程仍然会继续运行,直至结束。
如果一个线程确实需要实现能够被中断的要求,可以如此实现:
-
在正常运行时,需要一个无限循环来不断检查中断标识,当其为true时,则自行中断自己。
-
在调用阻塞方法时,正确处理InterruptException异常,例如捕获该异常然后结束该线程。
下面两段代码分别是处于等待状态和运行状态的线程响应interrupt的结果。
public class InterruptedTest {
public static void main(String[] args) {
//处于等待状态的线程, 当调用该线程的interrupt方法时,线程会抛出一个中断异常,但该线程仍处于存活状态
//以下是代码的输出结果:
//未执行interrupt:false
//执行完毕interrupt:false
//检测到中断
Thread waitThread = new Thread(new Runnable() {
@Override
public void run() {
while (true){
try {
Thread.sleep(10 * 1000); //让线程等待足够长时间
} catch (InterruptedException e) {
System.out.println("检测到中断");
}
}
}
}, "waitThread");
waitThread.start();
System.out.println("未执行interrupt:" + waitThread.isInterrupted());
waitThread.interrupt();
System.out.println("执行完毕interrupt:" + waitThread.isInterrupted());
}
}
public class InterruptedTest {
public static void main(String[] args) {
//处于运行状态的线程,当调用该线程的interrupt方法时,仅仅修改中断标识,其他任何事不做,此时线程仍处于存活状态
//以下是代码的输出结果:
//未执行interrupt:false
//执行完毕interrupt:true
Thread runningThread = new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (true){
count++;
}
}
}, "runningThread");
runningThread.start();
System.out.println("未执行interrupt:" + runningThread.isInterrupted());
runningThread.interrupt();
System.out.println("执行完毕interrupt:" + runningThread.isInterrupted());
}
}