package com.kuang.demo05;
public class TestJoin implements Runnable {@Overridepublic 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 {@Overridepublic 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
在这里插入代码片
