Use JUNIT4, GroboUtils multithreaded test
multithreaded programming and testing has been relatively downright thing, especially in multi-threaded test. Only fully tested before they can find BUG potential multi-threaded code. Here are just about a simple easy to use testing tool library for my own use when testing multi-threaded programs. That JUNIT4 and GroboUtils.
Ado, the code stickers out, we see to understand.
Reference article:
[url]
http://www.ibm.com/developerworks/cn/java/j-lo-test-multithread/index.html?ca=drs-
[/ url]
[url]
HTTP: // GroboUtils .sourceforge.net / index.html [/ url]
multithreaded programming and testing has been relatively downright thing, especially in multi-threaded test. Only fully tested before they can find BUG potential multi-threaded code. Here are just about a simple easy to use testing tool library for my own use when testing multi-threaded programs. That JUNIT4 and GroboUtils.
Ado, the code stickers out, we see to understand.
Java code
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.Hashtable;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
- import net.sourceforge.groboutils.junit.v1.TestRunnable;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MutiThreadTest {
- // Here you can declare some public variables
- static ApplicationContext context = null;
- static String[] path = new String[] { "" };
- static Map<String, String> countMap = new Hashtable<String, String>();
- static Map<String, String> countMap2 = new Hashtable<String, String>();
- static Set<String> countSet = new HashSet<String>();
- static List<String> list = new ArrayList<String>();
- @Before
- public void setUp() throws Exception {
- context = new ClassPathXmlApplicationContext(path);
- }
- @After
- public void tearDown() throws Exception {
- context = null;
- }
- /**
- * JUNIT runs this method, is the main thread
- */
- @Test
- public void testThreadJunit() throws Throwable {
- // TestRunnable, instantiate a custom 7 thread
- TestRunnable tr1, tr2, tr3, tr4, tr5, TR6, TR7;
- tr1 = new ThreadA();
- tr2 = new ThreadB();
- tr3 = new ThreadC();
- tr4 = new ThreadD();
- tr5 = new ThreadE();
- tr6 = new ThreadF();
- tr7 = new ThreadG();
- // must be declared as an array, pass parameters to the array when MultiThreadedTestRunner
- TestRunnable [] = {TRS TR1, TR2, TR3, TR4, TR5, TR6, TR7};
- // without changes
- MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
- // Perform MTTR and 7 thread
- mttr.runTestRunnables();
- }
- /**
- * To run multiple threads, the first to implement a custom thread </ br>
- I * as defined in A, B, C, D, E, F, G seven threads </ br>
- * Note: The custom thread must inherit TestRunnable </ br>
- * And covers the runTest () method
- *
- */
- private class ThreadA extends TestRunnable {
- @Override
- public void runTest() throws Throwable {
- Operation // thread to call or be executed
- myCommMethod2();
- }
- }
- private class ThreadB extends TestRunnable {
- @Override
- publicvoid runTest() throws Throwable {
- myCommMethod2();
- }
- }
- private class ThreadC extends TestRunnable {
- @Override
- publicvoid runTest() throws Throwable {
- myCommMethod2();
- }
- }
- private class ThreadD extends TestRunnable {
- @Override
- publicvoid runTest() throws Throwable {
- myCommMethod2();
- }
- }
- private class ThreadE extends TestRunnable {
- @Override
- publicvoid runTest() throws Throwable {
- myCommMethod2();
- }
- }
- private class ThreadF extends TestRunnable {
- @Override
- publicvoid runTest() throws Throwable {
- myCommMethod2();
- }
- }
- private class ThreadG extends TestRunnable {
- @Override
- publicvoid runTest() throws Throwable {
- myCommMethod2();
- }
- }
- /**
- * Methods thread to call. In this process </ br>
- * Achieve your multi-threaded code testing.
- * @throws Exception
- */
- public void myCommMethod2() throws Exception {
- System.out.println ( "thread ===" + Thread.currentThread () getId () +. "MyCommMethod2 perform operations beginning" );
- for (int i = 0; i <10; i++) {
- int a = i*5;
- System.out.println(a);
- }
- System.out.println ( "thread ===" + Thread.currentThread () getId () +. "MyCommMethod2 perform the operation ends" );
- }
- }
import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.Set; import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner; import net.sourceforge.groboutils.junit.v1.TestRunnable; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MutiThreadTest { //此处可以声明一些公共变量 static ApplicationContext context = null; static String[] path = new String[] { "" }; static Map<String, String> countMap = new Hashtable<String, String>(); static Map<String, String> countMap2 = new Hashtable<String, String>(); static Set<String> countSet = new HashSet<String>(); static List<String> list = new ArrayList<String>(); @Before public void setUp() throws Exception { context = new ClassPathXmlApplicationContext(path); } @After public void tearDown() throws Exception { context = null; } /** * JUNIT会运行这个方法,是主线程 */ @Test public void testThreadJunit() throws Throwable { //TestRunnable,实例化自定义的7个线程 TestRunnable tr1, tr2, tr3, tr4, tr5, tr6, tr7; tr1 = new ThreadA(); tr2 = new ThreadB(); tr3 = new ThreadC(); tr4 = new ThreadD(); tr5 = new ThreadE(); tr6 = new ThreadF(); tr7 = new ThreadG(); //必须声明为一个数组,把该数组当参数传递给 MultiThreadedTestRunner TestRunnable[] trs = { tr1, tr2, tr3, tr4, tr5, tr6, tr7 }; //不需改动 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs); //执行MTTR和7线程 mttr.runTestRunnables(); } /** * 要运行多线程,首先要实现自定义的线程</br> * 如下我定义了A,B,C,D,E,F,G七个线程</br> * 注意:自定义线程必须要继承TestRunnable</br> * 并且覆盖runTest()方法 * */ private class ThreadA extends TestRunnable { @Override public void runTest() throws Throwable { //线程要调用的方法或者要执行的操作 myCommMethod2(); } } private class ThreadB extends TestRunnable { @Override public void runTest() throws Throwable { myCommMethod2(); } } private class ThreadC extends TestRunnable { @Override public void runTest() throws Throwable { myCommMethod2(); } } private class ThreadD extends TestRunnable { @Override public void runTest() throws Throwable { myCommMethod2(); } } private class ThreadE extends TestRunnable { @Override public void runTest() throws Throwable { myCommMethod2(); } } private class ThreadF extends TestRunnable { @Override public void runTest() throws Throwable { myCommMethod2(); } } private class ThreadG extends TestRunnable { @Override public void runTest() throws Throwable { myCommMethod2(); } } /** * 线程要调用的方法。在此方法中</br> * 实现你的多线程代码测试。 * @throws Exception */ public void myCommMethod2() throws Exception { System.out.println("线程===" + Thread.currentThread().getId() + "执行myCommMethod2操作开始"); for (int i = 0; i <10; i++) { int a = i*5; System.out.println(a); } System.out.println("线程===" + Thread.currentThread().getId() + "执行myCommMethod2操作结束"); } }
Reference article:
[url]
http://www.ibm.com/developerworks/cn/java/j-lo-test-multithread/index.html?ca=drs-
[/ url]
[url]
HTTP: // GroboUtils .sourceforge.net / index.html [/ url]
- JUNIT multi-threaded test .rar (141.5 KB)
- Downloads: 104