栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【JAVA单元测试】模拟高并发操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【JAVA单元测试】模拟高并发操作

近期有需求需要在项目中模拟高并发,研究了下网上博友的方案,写了这篇较为详细的实施方案,供以后参考。【源码在文末,即取即用】

1.定义高并发请求数与倒计时器

新建测试类HighConcurrentTest.java, 定义变量:

	// 并发请求数
    private static final int threadNum =10;
    // 倒计时器
    private CountDownLatch cdl =new CountDownLatch(threadNum);
2.设计实现Runable接口的用户请求类(在HighConcurrentTest类内定义)
	public class UserRequest implements Runnable {

        public UserRequest() {
        }

        public UserRequest(int no) {
            this.no = no;
        }

        public int no;

        // 重写run方法用于处理业务逻辑
        @Override
        public void run() {
            try {
                cdl.await();
            }catch (Exception e) {
                e.printStackTrace();
            }

            // todo 业务逻辑...
            System.out.println("now no = " + no);
        }
    }
3.测试高并发
	@Test
    public void testConcurrent(){
        System.out.println("start ... ");
        for (int i =0; i< threadNum; i++) {
            new Thread(new UserRequest(i)).start();
            // 倒计时计数
            cdl.countDown();
        }


        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end ... ");
    }

执行后可发现日志顺序为:

start ... 
now no = 1
now no = 0
now no = 2
now no = 4
now no = 3
now no = 5
now no = 6
now no = 7
now no = 8
now no = 9
end ... 

说明线程间互不影响,模拟成功。

完整代码:

package xyz.dongzhensong.junitlearn.util;

import org.junit.Test;

import java.util.concurrent.CountDownLatch;

public class HighConcurrentTest {

    // 并发请求数
    private static final int threadNum =10;
    // 倒计时器
    private CountDownLatch cdl =new CountDownLatch(threadNum);

    public class UserRequest implements Runnable {

        public UserRequest() {
        }

        public UserRequest(int no) {
            this.no = no;
        }

        public int no;

        // 重写run方法用于处理业务逻辑
        @Override
        public void run() {
            try {
                cdl.await();
            }catch (Exception e) {
                e.printStackTrace();
            }

            // todo 业务逻辑...
            System.out.println("now no = " + no);
        }
    }

    @Test
    public void testConcurrent(){
        System.out.println("start ... ");
        for (int i =0; i< threadNum; i++) {
            new Thread(new UserRequest(i)).start();
            // 倒计时计数
            cdl.countDown();
        }


        try {
            Thread.sleep(5000);
            // 如线程内操作执行脚本,可先阻塞线程,等待子线程执行完成
//            Thread.currentThread().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end ... ");
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/728035.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号