当前位置:网站首页>Writing method of JUnit multithreading
Writing method of JUnit multithreading
2022-06-12 14:42:00 【Micro blog】
Junit Multithreading itself is not supported , You can use a third-party tool GroboUtils Realization .
The specific code is as follows :
@Test
publicvoidLessThanLimit()throwsInterruptedException{
// Construct a Runner
TestRunnable runner =newTestRunnable(){
@Override
publicvoid runTest()throwsThrowable{
// What to do
AccessId accessId =newAccessId(serviceId,group, version);// Provide the three elements of the service to be invoked
responeData =RPCConsumer.invoke(accessId, DATA);// Call the service
System.out.println(" The response message is :"+ responeData);
}
};
int runnerCount =5;
// Rnner Array , How many concurrent .
TestRunnable[] trs =newTestRunnable[runnerCount];
for(int i =0; i < runnerCount; i++){
trs[i]= runner;
}
// For executing multithreaded test cases Runner, Put the previously defined single Runner An array of components is passed in
MultiThreadedTestRunner mttr =newMultiThreadedTestRunner(trs);
try{
// Develop and execute the contents defined in the array concurrently
mttr.runTestRunnables();
}catch(Throwable e){
e.printStackTrace();
// You can judge the abnormal information here
}
// You can judge the return value of the code here
Assert.assertEquals(DATA, responeData);
}
Depend on GroboUtils Two of the packages
1、 download GroboUtils The toolkit Zip Package decompression , The directory structure after decompression is as follows

2、 Copy GroboUtils-5-core.jar、core In the directory GroboTestingJUnit-1.2.1-core.jar Two jar Package to local project for dependency

Java Multithreading
2018 year 11 month 5 Japan
17:36
java Multithreading is implemented in many ways , Once again, only one , By inheritance thread Class .
package test;
// By inheritance Thread Type implements multithreaded methods
public class ThreadDemo extends Thread {
static int result = 0;
public static void main(String[] args) throws InterruptedException {
// Initialize instance , And implement run Method ,start Is to perform run Method
ThreadDemo thread = new ThreadDemo();
thread.start();
// join Method means to wait for the thread to end
thread.join();
// The following code can achieve the same effect as the above two lines , If not run Method , Write the method name directly
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;
}
}
}
Method 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();
}
}
Method 3:
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.submit(new Runnable() {
@Override
public void run() {
}
});
executorService.submit(new Runnable() {
@Override
public void run() {
}
});
边栏推荐
- [gatewaysessioninactiveevent event] 1 Connect your own client disconnect
- 【SimpleDateFormat】1. Conversion of date type and text type 2 Thread unsafe
- Machine learning learning notes
- Huawei equipment is configured with H virtual private network
- And, or, not equal, operator
- C magic skill Chapter 4 (detailed explanation of memory function)
- ADSL
- [lambda operation jcf]
- Junit多线程的写法
- PMP敏捷知识点
猜你喜欢

能链智电登陆纳斯达克:贝恩是股东 成中国充电服务第一股

QT multi thread drawing and real-time refreshing method

Player practice 18 xresample

数据的收集

MAT的安装和使用

junit测试套件方法整理(方法二不太好用)

Machine learning learning notes

Appnium (II) installation and basic use of mitmproxy

QT to realize the simple use of SQLite database

基于TensorRT的深度学习模型部署实战教程!
随机推荐
C secret script Chapter 1: data storage (in-depth analysis) supplement
亿纬锂能拟募资90亿:刘金成骆锦红夫妇合计认购60亿 布局光谷
Common DOS commands
[wechat applet] 4 Introduction to wechat developer tools
【OCR】AspriseOCR C# 英文、數字識別(中文不行)
The igatewayloadbalancer interface implements routing. Otherwise, it follows Sid routing
Use Baidu AIP to obtain the text in the specified area of the screen
Junit多线程的写法
Two methods of implementing asynchronous calling function with QT
Mobileone: the mobile terminal only needs 1ms of high-performance backbone. You deserve it!
Ankai microelectronics rushes to the scientific innovation board: the annual revenue of 500million Xiaomi industry fund is the shareholder
Ppt cannot be opened, always prompt how to fix it
【Environment】1. Get the configuration in YML through the environment in the configuration class
【SimpleDateFormat】1. Conversion of date type and text type 2 Thread unsafe
C magic skill Chapter 4 (detailed explanation of memory function)
C secret arts script Chapter 5 (paragraph) (Section 3)
【Optional】1. Map and ifpresent 2 Ofnullable and orelse
[MySQL] basic database operation
Recursive summary of learning function
Reverse the encryption parameters of a hot water software