当前位置:网站首页>Junit多线程的写法
Junit多线程的写法
2022-06-12 14:18:00 【小小微的博客】
Junit本身是不支持多线程的,可以通过一个第三方的工具GroboUtils实现。
具体的代码如下:
@Test
publicvoidLessThanLimit()throwsInterruptedException{
// 构造一个Runner
TestRunnable runner =newTestRunnable(){
@Override
publicvoid runTest()throwsThrowable{
// 要执行的操作
AccessId accessId =newAccessId(serviceId,group, version);// 提供需要调用的服务三要素
responeData =RPCConsumer.invoke(accessId, DATA);// 调用服务
System.out.println("响应报文为:"+ responeData);
}
};
int runnerCount =5;
// Rnner数组,想当于并发多少个。
TestRunnable[] trs =newTestRunnable[runnerCount];
for(int i =0; i < runnerCount; i++){
trs[i]= runner;
}
// 用于执行多线程测试用例的Runner,将前面定义的单个Runner组成的数组传入
MultiThreadedTestRunner mttr =newMultiThreadedTestRunner(trs);
try{
// 开发并发执行数组里定义的内容
mttr.runTestRunnables();
}catch(Throwable e){
e.printStackTrace();
//可以在这里进行异常信息的判断
}
// 可以在这里判断代码的返回值
Assert.assertEquals(DATA, responeData);
}
需依赖GroboUtils的其中两个包
1、下载GroboUtils的工具包Zip包解压,解压后的目录结构如下

2、拷贝GroboUtils-5-core.jar、core目录中的GroboTestingJUnit-1.2.1-core.jar两个jar包到本地的项目中进行依赖

Java多线程
2018年11月5日
17:36
java多线程的实现由多种方法,再次仅介绍一种,通过继承thread类的方法实现。
package test;
//通过继承Thread类型实现多线程的方法
public class ThreadDemo extends Thread {
static int result = 0;
public static void main(String[] args) throws InterruptedException {
// 初始化实例,并执行run方法,start就是执行run方法
ThreadDemo thread = new ThreadDemo();
thread.start();
// join方法的意思是等待线程结束
thread.join();
// 下面的代码可以达到和上面两行同样的效果,如果不是run方法,则直接写方法名即可
ThreadDemo thread2 = new ThreadDemo();
thread2.start();
thread2.join();
System.out.println(result);
}
public void run1() {
for (int i = 0; i < 10000; i++) {
result = result + i;
}
}
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
result = result + i;
}
}
}
方法2
public class Test {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
// TODO
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
// TODO
}
});
t1.start();
t2.start();
}
}
方法3:
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.submit(new Runnable() {
@Override
public void run() {
}
});
executorService.submit(new Runnable() {
@Override
public void run() {
}
});
边栏推荐
- [OCR] aspriseocr C # English, number recognition (not Chinese)
- JD scanning code to obtain cookies
- Location (I) error: command erred out with exit status
- 我愿称之为史上最全的深度学习面经总结(附答案详解)
- Notepad common settings
- The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)
- QT multi thread drawing and real-time refreshing method
- C secret script Chapter 1: data storage (in-depth analysis) supplement
- Tu oses le croire? Il m'a fallu deux jours pour développer un système de gestion.
- Reverse order of Excel
猜你喜欢

面向优化科学研究领域的软件包
![[ROC] aspriseocr C # English, Digital identification (not Chinese)](/img/80/198145df663d2eeec6b8b1d7bc47b6.png)
[ROC] aspriseocr C # English, Digital identification (not Chinese)

The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)

Pay attention to click and pursue more users to enter the website. What bidding strategy can you choose?
![[Writeup]BUU SQL COURSE1[入门级]](/img/eb/1b2541b04ca231cb07f1f3706f51c7.png)
[Writeup]BUU SQL COURSE1[入门级]

Copy word content to excel and automatically divide it into multiple columns

高考回憶錄

How to realize the bidding strategy that pays more attention to transformation in the company's operation Google sem

Appnium (I) basic use of appnium

NetCore结合CAP事件总线实现分布式事务——入门(1)
随机推荐
QT realize picture dragging
Player practice 11 audio resampling
通信流量分析
The difference between parameter and argument in C language
Player practice 17 xvideowidget
Codeforces Round #798 (Div. 2)(A~D)
Appnium (I) basic use of appnium
Webdriver opens in full screen and a prompt "Chrome is under the control of automatic test software" appears in Chrome
JS (III) convert ES6 syntax to Es5 syntax
QT database realizes page turning function
Perfect ending | detailed explanation of the implementation principle of go Distributed Link Tracking
C secret script Chapter 3 (detailed explanation of string function) (Section 2)
Why do Chinese programmers change jobs?
我愿称之为史上最全的深度学习面经总结(附答案详解)
Machine learning learning notes
[OCR] aspriseocr C # English, number recognition (not Chinese)
Program analysis and Optimization - 6 loop optimization
华为设备配置BGP AS号替换
En langage C, la fonction principale appelle une autre fonction et assemble le Code pour comprendre
C secret script Chapter 1: data storage (in-depth analysis) supplement