Junit4 测试默认不能开启多线程,这里借助Groboutils Core可以实现多线程
下载jar包(不知道为什么我这边maven依赖不进去,只能导入jar包了):
https://mvnrepository.com/artifact/net.sourceforge.groboutils/groboutils-core/5
导入lib:
编写代码:
package com.wjj.application.utils;
import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
import net.sourceforge.groboutils.junit.v1.TestRunnable;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* RedLockUtils Tester.
* 锁测试
* @author hank
* @since <pre>11/05/2019</pre>
* @version 1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest
//@ContextConfiguration("/bootstrap.properties")
@ActiveProfiles(value="local")
@Transactional
@Rollback(false)
public class RedLockUtilsTest {
private String accountUnifiedorderLockKey = "ACCOUNT_PAY_UNIFIEDORDER_LOCK_KEY_TRADE_NO";
@Autowired
RedLockUtils redLockUtils;
@Autowired
DistributedLock distributedLock;
@Before
public void before() throws Exception {
}
@After
public void after() throws Exception {
}
@Test
public void testLock1() throws Throwable {
int sum = 8;
int Nsum = 100;
TestRunnable[] testRunnable = new TestRunnable[sum+Nsum];
for(int i=0; i< sum; i++){
//锁1线程
testRunnable[i] = new TestRunnable(){
@Override
public void runTest() throws Throwable {
System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000 +" start -----");
String lockKey = accountUnifiedorderLockKey + "unifiedorder" + "231123" + "sn1111125246546454564";
String identifier = distributedLock.lockWithTimeout(lockKey, 8000, 10000);
System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000+" lock :" + identifier + " -----");
Thread.sleep(2000);
if(StringUtils.isNotBlank(identifier)) {
distributedLock.releaseLock(lockKey, identifier);
System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000+" unlock -----");
}
}
};
}
for(int i=0; i< Nsum; i++){
//锁2线程
testRunnable[i+sum] = new TestRunnable(){
@Override
public void runTest() throws Throwable {
System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000 +" Nstart -----");
String lockKey = accountUnifiedorderLockKey + "unifiedorder" + "an465465464131232132" + "sn1111125246546454564";
boolean identifier = redLockUtils.tryLockTimeout(lockKey, 8000, 10000);
System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000+" Nlock :" + identifier + " -----");
Thread.sleep(2000);
if(identifier) {
redLockUtils.unLock(lockKey);
System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000+" Nunlock -----");
}
}
};
}
//加入并运行测试线程
new MultiThreadedTestRunner(testRunnable).runTestRunnables();
}
}