多线程测试

来源:互联网 发布:程序员客栈 接单 编辑:程序博客网 时间:2024/06/16 11:51
import org.junit.Test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import com.sf.tdop.container.manager.SequenceManager;import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;import net.sourceforge.groboutils.junit.v1.TestRunnable;@ContextConfiguration(locations = { "classpath:/test-service.xml" })public class SequenceTest extends AbstractJUnit4SpringContextTests{    private static final Logger logger = LoggerFactory.getLogger(SequenceTest.class);    @Autowired    SequenceManager sequenceManager;    @Test    public void generateCageNo() {        int threadNum = 100;//创建线程        TestRunnable runner = new TestRunnable() {            @Override            public void runTest() throws Throwable {                for (int i = 0; i < 100; i++) {                    logger.info("output     "+Integer.toString(sequenceManager.updateSequence("user")));                }            }        };//线程集合        TestRunnable[] trs = new TestRunnable[threadNum];        for (int i = 0; i < threadNum; i++) {            trs[i] = runner;        }        MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);        try {            mttr.runTestRunnables();        } catch (Throwable e) {            logger.error("多线程测试失败", e);        }    }}

xml:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <import resource="classpath*:META-INF/spring/*.xml" /></beans>

pom

<!-- junit 多线程测试 -->        <dependency>            <groupId>net.sourceforge.groboutils</groupId>            <artifactId>groboutils-core</artifactId>            <version>5</version>            <scope>test</scope>        </dependency>
原创粉丝点击