zoukankan      html  css  js  c++  java
  • Java高并发测试代码

    1、并发程序

    /**
     * 高并发测试:
     * 创建threadNum个线程;
     * 执行任务task
     * @param threadNum 线程数量
     * @param task 任务
     */
    public static void parallelTesk(int threadNum, Runnable task){
        
        // 1. 定义闭锁来拦截线程
        final CountDownLatch startGate = new CountDownLatch(1);
        final CountDownLatch endGate  = new CountDownLatch(threadNum);
    
        // 2. 创建指定数量的线程 
        for (int i = 0; i <threadNum; i++) {
            Thread t = new Thread(() -> {
                try {
                    startGate.await();
                    try {
                        task.run();
                    } finally {
                        endGate.countDown();
                    }
                } catch (InterruptedException e) {
    
                }
            });
    
            t.start();
        }
    
        // 3. 线程统一放行,并记录时间!
        long start =  System.nanoTime();
        
        startGate.countDown();
        try {
            endGate.await();
        } catch (InterruptedException e) {
        }
    
        long end = System.nanoTime();
        System.out.println("cost times :" +(end - start));
    }
    

    2、应用

    import java.util.concurrent.CountDownLatch;
    
    public class Main {
    
        /**
         * 定义一个不安全方法:在高并发情况下得不到想要的结果
         * queryTimes 是一个不安全的属性;
         * getTimes是一个不安全方法;
         */
        static int queryTimes = 0;
        public static int getTimes(){
            queryTimes = queryTimes +1;
            return queryTimes;
        }
    
        /**
         * 200个线程来执行获取getTimes
         * @param args
         */
        public static void main(String[] args) {
    
            parallelTesk(200, new Runnable() {
                @Override
                public void run() {
                    System.out.println(getTimes());
                }
            });
        }
    
        /**
         * 高并发测试:
         * 创建threadNum个线程;
         * 执行任务task
         * @param threadNum 线程数量
         * @param task 任务
         */
        public static void parallelTesk(int threadNum, Runnable task){
    
            // 1. 定义闭锁来拦截线程
            final CountDownLatch startGate = new CountDownLatch(1);
            final CountDownLatch endGate  = new CountDownLatch(threadNum);
    
            // 2. 创建指定数量的线程
            for (int i = 0; i <threadNum; i++) {
                Thread t = new Thread(() -> {
                    try {
                        startGate.await();
                        try {
                            task.run();
                        } finally {
                            endGate.countDown();
                        }
                    } catch (InterruptedException e) {
    
                    }
                });
    
                t.start();
            }
    
            // 3. 线程统一放行,并记录时间!
            long start =  System.nanoTime();
    
            startGate.countDown();
            try {
                endGate.await();
            } catch (InterruptedException e) {
            }
    
            long end = System.nanoTime();
            System.out.println("cost times :" +(end - start));
        }
        
    }
    
  • 相关阅读:
    DB2错误代码
    Hbase配置中出现的问题总结
    自定义控件三部曲之动画篇(一)——alpha、scale、translate、rotate、set的xml属性及用法
    关于CoordinatorLayout与Behavior的一点分析
    Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡...)
    android之官方下拉刷新组件SwipeRefreshLayout
    Android改变图片颜色的自定义控件
    android 之修改图片的某一颜色值
    Android动态修改图片颜色的实现方式分析
    Android WebView常见问题及解决方案汇总
  • 原文地址:https://www.cnblogs.com/dhcao/p/10970604.html
Copyright © 2011-2022 走看看