zoukankan      html  css  js  c++  java
  • main thread starting…

     执行结果例如以下:

      main thread starting…

      Thrad 2 staring…

      Thrad 2 end…

      Thrad 4 staring…

      Thrad 4 end…

      Thrad 1 staring…

      Thrad 1 end…

      Thrad 3 staring…

      Thrad 3 end…

      Thrad 5 staring…

      Thrad 5 end…

      main thread end…

      CountDownLatch方式代码例如以下:

      package com.test.thread;

      import java.util.concurrent.CountDownLatch;

      public class MyThread2 extends Thread

      {

      private CountDownLatch count;

      public MyThread2(CountDownLatch count, String name)

      {

      this.count = count;

      this.setName(name);

      }

      @Override

      public void run()

      {

      System.out.println(this.getName() + " staring…");

      System.out.println(this.getName() + " end…")。

      this.count.countDown();

      }

      /**

      * @param args

      */

      public static void main(String[] args)

      {

      System.out.println("main thread starting…");

      CountDownLatch count = new CountDownLatch(5);

      for (int i = 1; i <= 5; i++)

      {

      MyThread2 my = new MyThread2(count, "Thread " + i);

      my.start()。

      }

      try

      {

      count.await();

      }

      catch (InterruptedException e)

      {

      e.printStackTrace();

      }

      System.out.println("main thread end…");

      }

      }

      执行结果例如以下:

      main thread starting…

      Thread 2 staring…

      Thread 2 end…

      Thread 4 staring…

      Thread 4 end…

      Thread 1 staring…

      Thread 1 end…

      Thread 3 staring…

      Thread 3 end…

      Thread 5 staring…

      Thread 5 end…

      main thread end…

  • 相关阅读:
    What's different between Ctrl+Z and Ctrl+C in Unix command line?
    bitbucket使用,经验总结
    SpringMVC 强大的注解方式,走一个流程,看看,这些注解用过么
    java异常处理,需要考虑的流程
    两个日期之间相差的天数
    vue 路由面试题
    promise
    es6面试题
    谈谈你是如何理解 JS 异步编程的,EventLoop、消息队列都是做什么的,什么是宏任务,什么是微任务?
    使用高阶函数实现Array对象的方法
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/7100786.html
Copyright © 2011-2022 走看看