Use Java and Scala to study concurrent problem.
This repo is associated with Oxford ACS concurrent algorithms and data structures
线程的执行顺序是不确定的 调用Thread的start()方法启动线程时,线程的执行顺序是不确定的。也就是说,在同一个方法中,连续创建多个线程后,调用线程的start()方法的顺序并不能决定线程的执行顺序。
例如,这里,看一个简单的示例程序,如下所示。
/**
* @description 线程的顺序,直接调用Thread.start()方法执行不能确保线程的执行顺序
*/
public class ThreadSort01 {
public static void main(String[] args){
Thread thread1 = new Thread(() -> {
System.out.println("thread1");
});
Thread thread2 = new Thread(() -> {
System.out.println("thread2");
});
Thread thread3 = new Thread(() -> {
System.out.println("thread3");
});
thread1.start();
thread2.start();
thread3.start();
}
}
在ThreadSort01类中分别创建了三个不同的线程,thread1、thread2和thread3,接下来,在程序中按照顺序分别调用thread1.start()、thread2.start()和thread3.start()方法来分别启动三个不同的线程。
那么,问题来了,线程的执行顺序是否按照thread1、thread2和thread3的顺序执行呢?运行ThreadSort01的main方法,结果如下所示。
thread1
thread2
thread3
再次运行时,结果如下所示。
thread1
thread3
thread2
第三次运行时,结果如下所示。
thread2
thread3
thread1
可以看到,每次运行程序时,线程的执行顺序可能不同。线程的启动顺序并不能决定线程的执行顺序。
加了synchronized还是无法确保按照顺序执行,因为有可能时间片先给t2
使用join可以保证顺序进行
@Slf4j(topic = "waitDemo")
public class waitNotifyDemo {
static Object lock = new Object();
public static void main(String[] args) throws InterruptedException {
for(int i =0;i<1000;i++){
Thread t1 = new Thread(()->{
// synchronized (lock){
log.debug("| running");
// }
},"t1");
Thread t2 =new Thread(()->{
// synchronized (lock){
log.debug("- running");
// }
},"t2");
t1.start();
t1.join();
t2.start();
t2.join();
}
}
}
线程八锁https://www.bilibili.com/video/BV16J411h7Rd?p=60&vd_source=8d4c66e89257a55f9732ff93790086b8
这里先1后2 或者先 2后1 都有可能,synchronized的作用是互斥,没法推断先后顺序
synchronized加在方法上,锁的是this对象 synchronized的作用是互斥,所以后面即使线程1休眠,线程2也会在其休眠时等待 这里先1后2 或者先 2后1 都有可能