package com.kuang.demo05;
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
System.out.println("VIP线程来插队了!!!"+i);
}
}
public static void main(String[] args) throws InterruptedException {
// Thread thread= new Thread(new TestJoin());
new Thread(new TestJoin()).start();
for (int i = 0; i <400 ; i++) {
System.out.println("主线程在排队!!!"+i);
if (i==100){
new Thread(new TestJoin()).join();
}
}
}
}
没有按预期进行强制执行
1.原因每次强制执行都是新new()了一个线程
2.在插队之前都是同步进行执行的
解决代码
package com.kuang.demo05;
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i <1000 ; i++) {
System.out.println("VIP线程来插队了!!!"+i);
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin testJoin = new TestJoin();
Thread thread= new Thread(testJoin);
thread.start();
for (int i = 0; i <500 ; i++) {
if (i==200){
thread.join();
}
System.out.println("主线程在排队!!!"+i);
}
}
}
```java
在这里插入代码片
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201223124821993.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NzgzNjYw,size_16,color_FFFFFF,t_70)