JUNIT4 GroboUtils multi-threaded test

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.
Java code   Collection Code
  1. import java.util.ArrayList;  
  2. import java.util.HashSet;  
  3. import java.util.Hashtable;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import java.util.Set;  
  7.   
  8. import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;  
  9. import  net.sourceforge.groboutils.junit.v1.TestRunnable;  
  10.   
  11. import org.junit.After;  
  12. import org.junit.Before;  
  13. import org.junit.Test;  
  14. import org.springframework.context.ApplicationContext;  
  15. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  16.   
  17. public class MutiThreadTest {  
  18.     // Here you can declare some public variables  
  19.     static ApplicationContext context = null;  
  20.     static String[] path = new String[] { "" };  
  21.     static Map<String, String> countMap = new Hashtable<String, String>();  
  22.     static Map<String, String> countMap2 = new Hashtable<String, String>();  
  23.     static Set<String> countSet = new HashSet<String>();  
  24.     static List<String> list = new ArrayList<String>();  
  25.   
  26.     @Before  
  27.     public void setUp() throws Exception {  
  28.         context = new ClassPathXmlApplicationContext(path);  
  29.     }  
  30.   
  31.     @After  
  32.     public void tearDown() throws Exception {  
  33.         context = null;  
  34.     }  
  35.             /** 
  36.      * JUNIT runs this method, is the main thread 
  37.      */  
  38.     @Test  
  39.     public void testThreadJunit() throws Throwable {  
  40.         // TestRunnable, instantiate a custom 7 thread  
  41.         TestRunnable tr1, tr2, tr3, tr4, tr5, TR6, TR7;  
  42.         tr1 = new ThreadA();  
  43.         tr2 = new ThreadB();  
  44.         tr3 = new ThreadC();  
  45.         tr4 = new ThreadD();  
  46.         tr5 = new ThreadE();  
  47.         tr6 = new ThreadF();  
  48.         tr7 = new ThreadG();  
  49.         // must be declared as an array, pass parameters to the array when MultiThreadedTestRunner  
  50.         TestRunnable [] = {TRS TR1, TR2, TR3, TR4, TR5, TR6, TR7};  
  51.         // without changes  
  52.         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);  
  53.         // Perform MTTR and 7 thread  
  54.         mttr.runTestRunnables();  
  55.     }  
  56.       
  57.     /** 
  58.      * To run multiple threads, the first to implement a custom thread </ br> 
  59.      I * as defined in A, B, C, D, E, F, G seven threads </ br> 
  60.      * Note: The custom thread must inherit TestRunnable </ br> 
  61.      * And covers the runTest () method 
  62.      * 
  63.      */  
  64.     private class ThreadA extends TestRunnable {  
  65.         @Override  
  66.         public void runTest() throws Throwable {  
  67.             Operation // thread to call or be executed  
  68.             myCommMethod2();  
  69.         }  
  70.     }  
  71.   
  72.     private class ThreadB extends TestRunnable {  
  73.         @Override  
  74.         publicvoid runTest() throws Throwable {   
  75.             myCommMethod2();  
  76.         }  
  77.     }  
  78.   
  79.     private class ThreadC extends TestRunnable {  
  80.         @Override  
  81.         publicvoid runTest() throws Throwable {   
  82.             myCommMethod2();  
  83.         }  
  84.     }  
  85.   
  86.     private class ThreadD extends TestRunnable {  
  87.         @Override  
  88.         publicvoid runTest() throws Throwable {   
  89.             myCommMethod2();  
  90.         }  
  91.     }  
  92.   
  93.     private class ThreadE extends TestRunnable {  
  94.         @Override  
  95.         publicvoid runTest() throws Throwable {   
  96.             myCommMethod2();  
  97.         }  
  98.     }  
  99.   
  100.     private class ThreadF extends TestRunnable {  
  101.         @Override  
  102.         publicvoid runTest() throws Throwable {   
  103.             myCommMethod2();  
  104.         }  
  105.     }  
  106.   
  107.     private class ThreadG extends TestRunnable {  
  108.         @Override  
  109.         publicvoid runTest() throws Throwable {   
  110.             myCommMethod2();  
  111.         }  
  112.     }  
  113.   
  114.     /** 
  115.      * Methods thread to call. In this process </ br> 
  116.      * Achieve your multi-threaded code testing. 
  117.      * @throws Exception 
  118.      */  
  119.     public void myCommMethod2() throws Exception {  
  120.         System.out.println ( "thread ==="  + Thread.currentThread () getId () +.  "MyCommMethod2 perform operations beginning" );  
  121.         for (int i = 0; i <10; i++) {  
  122.              int a  = i*5;  
  123.              System.out.println(a);  
  124.         }  
  125.         System.out.println ( "thread ==="  + Thread.currentThread () getId () +.  "MyCommMethod2 perform the operation ends" );  
  126.     }  
  127. }  

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]

Guess you like

Origin www.cnblogs.com/jpfss/p/11582618.html