join()
    当前线程暂停, 等待指定的线程执行结束后, 当前线程再继续
    join(int)
    可以等待指定的毫秒之后继续

    1. public class Demo06Join {
    2. public static void main(String[] args) {
    3. final Thread t1 = new Thread("======t1======") {
    4. @Override
    5. public void run() {
    6. for(int i = 0; i < 1000; i++) {
    7. System.out.println(getName());
    8. }
    9. }
    10. };
    11. Thread t2 = new Thread("t2") {
    12. @Override
    13. public void run() {
    14. for(int i = 0; i < 10; i++) {
    15. if(i == 2) {
    16. try {
    17. System.out.println("线程t1插队执行");
    18. //t1.join();
    19. t1.join(1); //插队指定的时间,过了指定时间后,两条线程交替执行
    20. } catch (InterruptedException e) {
    21. e.printStackTrace();
    22. }
    23. }
    24. System.out.println(getName());
    25. }
    26. }
    27. };
    28. t1.start();
    29. t2.start();
    30. }
    31. }
    1. t2
    2. t2
    3. 线程======t1======插队执行
    4. ======t1======
    5. ======t1======
    6. ======t1======
    7. ======t1======
    8. ======t1======
    9. ......
    10. ......
    11. ......