`

统计多线程下程序运行总时间

    博客分类:
  • java
阅读更多

package com.gpcsoft.hct.epp.egp.thread;

import com.gpcsoft.hct.epp.egp.HttpClientBailApi;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

/**
* @Auther: gaojp
* @Date: 2019/5/5 17:14
* @Description:
*/

public class ThreadTest {

    public static Boolean printStatusInfo = false;
    public void mutiThread(){
        long start = Calendar.getInstance().getTimeInMillis();
        //线程数量
        int num =100;
        Boolean printStatus =false;
        //计数
        CountDownLatch countDownLatch = new CountDownLatch(num);
        //停车栅栏
        CyclicBarrier cyclicBarrier = new CyclicBarrier(num);

        for (int i = 0; i <num ; i++) {
            String mobile ="200"+i;
            String tag =mobile+"_"+ Calendar.getInstance().getTimeInMillis();
            new Thread(new ThreadNum(countDownLatch, mobile,tag,cyclicBarrier,start,printStatus)).start();
            countDownLatch.countDown();

        }
    }


    private class ThreadNum implements Runnable {
        private CountDownLatch countDownLatch;
        private CyclicBarrier cyclicBarrier;
        private long start;
        private boolean printStatus;

        private String mobile;
        private String tag;

        private ThreadNum(CountDownLatch countDownLatch, String mobile, String tag) {
            this.countDownLatch = countDownLatch;
            this.mobile = mobile;
            this.tag = tag;
        }

        private ThreadNum(CountDownLatch countDownLatch, String mobile, String tag,CyclicBarrier cyclicBarrier) {
            this.countDownLatch = countDownLatch;
            this.mobile = mobile;
            this.tag = tag;
            this.cyclicBarrier =cyclicBarrier;
        }


        private ThreadNum(CountDownLatch countDownLatch, String mobile, String tag,
                          CyclicBarrier cyclicBarrier,boolean printStatus) {
            this.countDownLatch = countDownLatch;
            this.mobile = mobile;
            this.tag = tag;
            this.cyclicBarrier =cyclicBarrier;
            this.printStatus = printStatus;
        }

        private ThreadNum(CountDownLatch countDownLatch, String mobile, String tag,
                          CyclicBarrier cyclicBarrier,long start,boolean printStatus) {
            this.countDownLatch = countDownLatch;
            this.mobile = mobile;
            this.tag = tag;
            this.cyclicBarrier =cyclicBarrier;
            this.start = start;
            this.printStatus = printStatus;
        }

        @Override
        public void run() {

            try {
                //等待所有线程准备完毕后,同时访问接口
                countDownLatch.await();
                Map<String,String> req = new HashMap<String,String>();
                req.put("username","little");
                req.put("password","1992");
                Iterator<String> item = req.values().iterator();
                StringBuffer sb = new StringBuffer();
                while (item.hasNext()){
                    sb.append(item.next());
                }
                System.out.println(sb.toString());
                String url ="http://localhost:9090/shiro/loginUser";
                HttpClientBailApi api = new HttpClientBailApi();
                try {
                    String string = api.postStringApi(url, req);
                    System.out.println("result:::"+string.substring(0,10)+"___"+mobile+"-"+tag);
                }catch (Exception ex){
                    ex.printStackTrace();
                }

                //等待线程运行完成
               int awaitNum = cyclicBarrier.await();
                long end = Calendar.getInstance().getTimeInMillis();
                if(!printStatusInfo) {
                    printStatusInfo = true;
                    System.out.println(awaitNum + "运行时间::" + (end - start));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ThreadTest  test = new ThreadTest();
        test.mutiThread();

    }

}




分享到:
评论

相关推荐

    5操作系统实验报告.doc

    课 程 实 验 报 告 课程名称: 线程控制实验 ... 6、让单线程程序睡眠四秒钟、多线程程序各子线程睡一秒的条件下(两程序的 总睡眠时间相同),对比执行结果; 7、分析两次对比结果的差异,写出自己的见解。 三、运行

    操作系统实验报告-.docx

    6、让单线程程序睡眠四秒钟、多线程程序各子程序睡一秒的条件下(两个程序总的睡眠时间相同),对比执行结果; 操作系统实验报告-全文共5页,当前为第1页。 7、分析两次对比结果的差异,写出自己的见解。 四、实验...

    C++Builder精彩编程实例集锦的源代码前3部分.rar

    实例055 如何创建多线程程序 实例056 如何创建可独立执行程序 实例057 如何编写单文档应用程序 实例058 如何编写多文档应用程序 实例059 如何编写COM服务器程序 实例060 如何编写COM客户端程序 实例061 如何...

    C++Builder精彩编程实例集锦的源代码后3部分.rar

    实例055 如何创建多线程程序 实例056 如何创建可独立执行程序 实例057 如何编写单文档应用程序 实例058 如何编写多文档应用程序 实例059 如何编写COM服务器程序 实例060 如何编写COM客户端程序 实例061 如何...

    第一部分 界面设计

    实例055 如何创建多线程程序 实例056 如何创建可独立执行程序 实例057 如何编写单文档应用程序 实例058 如何编写多文档应用程序 实例059 如何编写COM服务器程序 实例060 如何编写COM客户端程序 实例061 如何...

    java课程设计报告---万年历.doc

    Java支持多线程编程,Java运行时系统在多线程同步方面具有成熟的解 决方案。Java的平台标准有Java ME,Java SE和Java EE。Java发展到今天,它的卓越成就及在业界的地位毋庸置疑。目前在众多的支持Java 的开发工具中...

    Visual C++通用范例开发金典(第三卷/共三卷)

    主要内容包括:用户界面设计、文档应用程序设计、图形程序设计、文件I/O与串行化程序设计、多媒体程序设计、系统控制与注册表、数据库技术程序设计、网络、通信技术程序设计、打印程序设计、线程和进程程序设计、...

    Visual C++通用范例开发金典(分卷二/共三卷)

    主要内容包括:用户界面设计、文档应用程序设计、图形程序设计、文件I/O与串行化程序设计、多媒体程序设计、系统控制与注册表、数据库技术程序设计、网络、通信技术程序设计、打印程序设计、线程和进程程序设计、...

    Visual C++通用范例开发金典(第一卷、共两卷)

    主要内容包括:用户界面设计、文档应用程序设计、图形程序设计、文件I/O与串行化程序设计、多媒体程序设计、系统控制与注册表、数据库技术程序设计、网络、通信技术程序设计、打印程序设计、线程和进程程序设计、...

    基于winpcap发送数据包

    04、使用多线程发包,在XP下最多可以同时开2000个线程发包,发包速度快,流量大。在千兆网卡上可以超过15M/s的速度。 05、自动统计发包时间,计算每秒发包数量,流量。 06、可以设置发包的时间间隔,最少到1...

    JAVA课程设计之赛马游戏.doc

    能统计每匹马所跑完所需的总时间,最终的名次。 3. 支持用户对马进行投注,并记录输赢,保存到文件或数据库中。 b.从这个题目来分析,要做的主要有四大点: 1.第一点就是要设计用户登录赛马的页面,登陆后才能进行...

    delphi 开发经验技巧宝典源码

    0149 使用ForegroundTask函数检测线程是否运行 96 第5章 字符与字符串处理技术 99 5.1 ASCII码与编码转换 100 0150 如何获得汉字的区位码 100 0151 通过区位码获取汉字 100 0152 根据ASCII码获得字母 ...

    Oracle9i的init.ora参数中文说明

    说明: 指定多个监听程序的地址是分别指定的, 还是用一个 ADDRESS_LIST 字符串指定。如果该值为 TRUE, MTS_LISTENER_ADDRESS 参数可被指定为: (ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(PORT=5000)(HOST=zeus)) ...

    asp.net知识库

    ASP.Net应用程序的多进程模型 NET委托:一个C#睡前故事 [推荐] - [原创] Microsoft .NET策略及框架概述 卸载Class? Web Form 窗体 如何实现web页面的提示保存功能 在ASP.Net中两种利用CSS实现多界面的方法 如何在...

    Python Cookbook

    17.5 在多线程环境中使用SWIG生成的模块 603 17.6 用PySequence_Fast将Python序列转为 C数组 604 17.7 用迭代器逐个访问Python序列的元素 608 17.8 从Python可调用的C函数中返回None 611 17.9 用gdb调试动态载入...

Global site tag (gtag.js) - Google Analytics