zoukankan      html  css  js  c++  java
  • CountDownLatch模拟高并发测试代码

    直接上代码进行验证吧

    /**
     * 通过countdownlatch的机制,来实现并发运行
     * 模拟200个并发测试
    * @author ll
    * @date 2018年4月18日 下午3:55:59
     */
    public class CountDownLatchTest {
        
        private static final int threadnum = 200;
        
        public CountDownLatch cd = new CountDownLatch(threadnum);
          
        @Test
        public void TestCountDownlatch() throws InterruptedException{
            for (int i = 0; i < threadnum; i++) {
                new Thread(new Request()).start();
                cd.countDown();
            }
            /*
             * 在多线程编程中,Thread.CurrentThread 表示获取当前正在运行的线程,join方法是阻塞当前调用线程,
             * 直到某线程完全执行才调用线程才继续执行,
             * 如果获取的当前线程是主线程,调用Join方法,阻塞主线程
             */
            Thread.currentThread().join();
            
        }
        
        class Request implements Runnable{
            
            @Override
            public void run() {
                try {
                    cd.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
               System.out.println(Thread.currentThread()+"get one,"+System.currentTimeMillis());
            }
            
        }
        
    }
  • 相关阅读:
    Java学习:冒泡排序和选择排序
    Java学习:多态
    Java学习:抽象类与接口
    Java学习:继承
    Java学习:标准类
    Java学习:方法简介
    传参的本质
    new 关键字做的事
    一个引用类型的对象占多大堆空间
    栈中空间大小
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/8979628.html
Copyright © 2011-2022 走看看