1. public class Test {
    2. public static void main(String[] args) {
    3. Thread t = new Thread(() -> {
    4. try {
    5. Thread.sleep(2000);
    6. } catch (InterruptedException e) {
    7. e.printStackTrace();
    8. }
    9. System.out.println("-------");
    10. });
    11. t.start();
    12. // 调用了start方法且线程没死亡返回true
    13. while (t.isAlive()) {
    14. }
    15. System.out.println("main");
    16. }
    17. }
    1. public class Test {
    2. public static void main(String[] args) throws InterruptedException {
    3. Thread t = new Thread(() -> {
    4. try {
    5. Thread.sleep(2000);
    6. } catch (InterruptedException e) {
    7. e.printStackTrace();
    8. }
    9. System.out.println("-------");
    10. });
    11. t.start();
    12. t.join();
    13. System.out.println("main");
    14. }
    15. }
    1. public class Test {
    2. public static void main(String[] args) throws InterruptedException {
    3. CountDownLatch countDownLatch = new CountDownLatch(1);
    4. Thread t = new Thread(() -> {
    5. try {
    6. Thread.sleep(2000);
    7. } catch (InterruptedException e) {
    8. e.printStackTrace();
    9. }
    10. System.out.println("-------");
    11. countDownLatch.countDown();
    12. });
    13. t.start();
    14. countDownLatch.await();
    15. System.out.println("main");
    16. }
    17. }
    1. public class Test {
    2. public static void main(String[] args) throws ExecutionException, InterruptedException {
    3. Callable<Integer> myCallable = new MyCallable();
    4. FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
    5. Thread thread = new Thread(futureTask);
    6. thread.start();
    7. futureTask.get();
    8. System.out.println("main");
    9. }
    10. }
    11. class MyCallable implements Callable<Integer> {
    12. @Override
    13. public Integer call() throws InterruptedException {
    14. Thread.sleep(5000);
    15. System.out.println("-----------");
    16. return 1;
    17. }
    18. }
    1. public class Test {
    2. public static void main(String[] args) throws InterruptedException {
    3. BlockingQueue queue = new ArrayBlockingQueue(1);
    4. Thread t = new Thread(() -> {
    5. try {
    6. Thread.sleep(2000);
    7. } catch (InterruptedException e) {
    8. e.printStackTrace();
    9. }
    10. System.out.println("-------");
    11. try {
    12. queue.put("1");
    13. } catch (InterruptedException e) {
    14. e.printStackTrace();
    15. }
    16. });
    17. t.start();
    18. queue.take();
    19. System.out.println("main");
    20. }
    21. }
    1. public class Test {
    2. public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
    3. CyclicBarrier barrier = new CyclicBarrier(2);
    4. Thread t = new Thread(() -> {
    5. try {
    6. Thread.sleep(2000);
    7. } catch (InterruptedException e) {
    8. e.printStackTrace();
    9. }
    10. System.out.println("-------");
    11. try {
    12. barrier.await();
    13. } catch (InterruptedException e) {
    14. e.printStackTrace();
    15. } catch (BrokenBarrierException e) {
    16. e.printStackTrace();
    17. }
    18. });
    19. t.start();
    20. barrier.await();
    21. System.out.println("main");
    22. }
    23. }
    1. public class Test {
    2. public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
    3. Thread mainThread = Thread.currentThread();
    4. Thread t = new Thread(() -> {
    5. try {
    6. Thread.sleep(2000);
    7. } catch (InterruptedException e) {
    8. e.printStackTrace();
    9. }
    10. System.out.println("-------");
    11. LockSupport.unpark(mainThread);
    12. });
    13. t.start();
    14. LockSupport.park();
    15. System.out.println("main");
    16. }
    17. }