文章目录
1、线程的等待
多个线程调度的顺序,在系统中是无序的(抢占式执行)
多个线程什么时候被调度执行不确定,多个线程谁先执行结束也不确定
所有我们可以通过线程等待,来确定线程结束的先后顺序
1.1 Thread.join()方法
join()方法是Thread类的一个方法,用于让一个线程等待另一个线程执行结束,当一个线程调用join()方法时,当前线程就会阻塞,直到被调用的线程执行结束
举例:
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()-> {
for (int i = 0; i < 5; i++) {
System.out.println("hello thread");
}
});
thread.start();
thread.join();
System.out.println("hello main");
}
}
运行结果:
在main线程中调用thread.join()就是让main等待thread,也就是thread先结束,main后结束
main线程中调用thread.join()方法,有以下两种可能:
1.如果thread线程此时结束了,那么join就会立即返回
2.如果thread线程此时还没有结束,join就会阻塞等待,一直等待到thread线程结束之后,join才能解除阻塞,继续执行
1.1.1 main线程中等待多个线程
main线程中调用多个join(),先执行thread1.join(),如果thread1还没有结束,thread1.join()就阻塞等待,thread1结束之后,main继续执行thread2.join(),再等待thread2结束
注意:thread1和thread2是抢占式执行的,所有谁写在前面都是一样的
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(()-> {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("hello thread1");
}
});
Thread thread2 = new Thread(()-> {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("hello thread2");
}
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("hello main");
}
}
运行结果:
1.2.2 main线程等待thread2线程且thread2线程等待thread1线程
public class Test13 {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(()-> {
for (int i = 0; i < 3; i++) {
System.out.println("thread1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
Thread thread2 = new Thread(()-> {
try {
thread1.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
for (int i = 0; i < 3; i++) {
System.out.println("thread2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
thread1.start();
thread2.start();
thread2.join();
System.out.println("main end");
}
}
运行结果:
先创建thread1线程,再创建thread2线程,thread1没有阻塞直接执行代码,而thread2遇到阻塞,等待thread1执行完毕之后,thread2解除阻塞,由于main线程阻塞,所以要等待thread2线程执行完毕,当thread2线程执行完毕之后,main线程解除阻塞,执行main线程中的代码
1.1.3 其它线程阻塞等待main线程
public class Test14 {
public static void main(String[] args) throws InterruptedException {
//获取当前的main线程
Thread t = Thread.currentThread();
//打印获取到的线程的名字
System.out.println(t.getName());
Thread t2 = new Thread(() -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t2 结束");
});
t2.start();
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
}
System.out.println("main 结束");
}
}
运行结果:
t2线程阻塞,等待main线程执行完毕之后,再执行t2线程
1.1.4 在指定时间内阻塞等待
方法 | 说明 |
---|---|
public void join() | 等待线程结束 |
public void join(long millis) | 等待线程结束,最多等millis毫秒 |
public void join(long millis, int nanos) | 同理,可以更高精度 |
join(long millis),表示当前线程最多等待 millis 毫秒,如果超过这个时间 thread 线程还没有执行完毕,当前线程会继续往下执行,简单来说,即使 thread 这个线程还没有结束,main 线程都不会继续等待了;如果 thread 在规定的时间内提前结束,那么 main 也会提前解除阻塞
举例:
public class Test15 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()-> {
for (int i = 0; i < 2; i++) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("Thread end");
});
thread.start();
thread.join(3000);
System.out.println("main end");
}
}
运行结果:
1.2 Thread.sleep()方法
使用sleep控制的是“线程休眠的时间”而不是“两个代码执行的间隔时间”该方法接受一个以毫秒为单位的时间参数,指定线程阻塞的时间长度。在这段时间内,线程是一定不会去cpu上执行的,当时间到了之后,线程从阻塞状态恢复到就绪状态,这里不代表线程就立即去cpu上执行,从恢复就绪到真正去cpu上执行,还是需要一定的时间的
2、线程的状态
在Java中对线程的状态,大概分成了6种不同的状态
2.1 新建状态-NEW
Thread对象有了,还没有调用start,系统内部的线程还未创建
2.2 终止状态-TERMINATED
线程已经终止了,内核中的线程已经销毁了,Thread对象还在
2.3 就绪状态-RUNNABLE
指的是,这个线程“随叫随到”,有两种就绪状态
1.这个线程正在cpu上执行
2.这个线程虽然没有在cpu上执行,但是随时可以调度到cpu上执行
2.4 等待状态-WAITING
线程进入等待状态通常是因为调用了Thread.join()等方法
代码如下:
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()-> {
while (true) {
System.out.println("hello thread");
}
});
thread.start();
thread.join();
System.out.println("hello main");
}
}
2.5 超时等待状态-TIMED_WAITING
线程调用带有超时参数的等待方法,比如 Thread.sleep(long millis) 等待方法。线程会进入超时等待状态下,线程会等待一段时间后自动恢复到就绪状态
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()-> {
while (true) {
System.out.println("hello thread");
try {
Thread.sleep(3600*1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
thread.start();
System.out.println("hello main");
}
}
2.6 阻塞状态-BLOCKED
线程在特定情况下,会进入阻塞状态,比如调用了 Thread.sleep() 方法或者加锁。在线程阻塞状态下,线程暂时停止执行,直到满足特定条件后,才能继续执行,这里不做过多介绍,后续还会说明
代码如下:
public class Test {
public static void main(String[] args) {
Object locker1 = new Object();
Object locker2 = new Object();
Thread thread1 = new Thread(()->{
synchronized (locker1){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (locker2){
System.out.println("正在执行 t1 线程");
}
}
});
Thread thread2 = new Thread(()->{
synchronized (locker2){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (locker1){
System.out.println("正在执行 t2 线程");
}
}
});
thread1.start();
thread2.start();
}
}