Java多线程调用

  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import java.util.concurrent.Callable;  
  6. import java.util.concurrent.Executors;  
  7. import java.util.concurrent.Future;  
  8. import java.util.concurrent.ThreadPoolExecutor;  
  9.   
  10. import mtp.lr.test.base.Base;  
  11.   
  12. public class JavaThreadPool {  
  13.     private ThreadPoolExecutor pool = null;  
  14.     private Map<String, Future<String>> resultMap = new HashMap<String, Future<String>>();  
  15.     private List<String> list = new ArrayList<String>();  
  16.     private long tid = 0;  
  17.     private SubCall st = null;  
  18.     int callCount=1;  
  19.   
  20.     public JavaThreadPool(int poolMax) {  
  21.         pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(poolMax);  
  22.   
  23.         // TODO Auto-generated constructor stub  
  24.     }  
  25.   
  26.     public void putThread(Base base) {  
  27.         list.add(String.valueOf(tid));  
  28.         resultMap.put(String.valueOf(tid), submitThread(base));  
  29.         tid++;  
  30.     }  
  31.   
  32.     public void putThread(Base base, String tmsg) {  
  33.         list.add(String.valueOf(tid) + ":" + tmsg);  
  34.         resultMap.put(String.valueOf(tid) + ":" + tmsg, submitThread(base));  
  35.         tid++;  
  36.     }  
  37.   
  38.     public void putThread(Callable<String> sc) {  
  39.         list.add(String.valueOf(tid));  
  40.         resultMap.put(String.valueOf(tid), submitThread(sc));  
  41.         tid++;  
  42.     }  
  43.   
  44.     public void putThread(Callable<String> sc, String tmsg) {  
  45.         list.add(String.valueOf(tid) + ":" + tmsg);  
  46.         resultMap.put(String.valueOf(tid) + ":" + tmsg, submitThread(sc));  
  47.         tid++;  
  48.     }  
  49.   
  50.     private Future<String> submitThread(Base base) {  
  51.         st = new SubCall(base);  
  52.         Future<String> f = (Future<String>) pool.submit(st);  
  53.         return f;  
  54.     }  
  55.   
  56.     private Future<String> submitThread(Callable<String> sc) {  
  57.         Future<String> f = (Future<String>) pool.submit(sc);  
  58.         return f;  
  59.     }  
  60.   
  61.     public void shutdown() {  
  62.         pool.shutdown();  
  63.     }  
  64.   
  65.     public void shutdownNow() {  
  66.         pool.shutdownNow();  
  67.     }  
  68.   
  69.     public Future<String> getTResult(int i) {  
  70.         return resultMap.get(this.getTid(i));  
  71.     }  
  72.   
  73.     public String getTid(int i) {  
  74.         return this.list.get(i);  
  75.     }  
  76.   
  77.     public void removeResult(int i) {  
  78.         resultMap.remove(list.get(i));  
  79.         list.remove(i);  
  80.     }  
  81.   
  82.     /** 
  83.      * @return the resultMap 
  84.      */  
  85.     public Map<String, Future<String>> getResultMap() {  
  86.         return resultMap;  
  87.     }  
  88.   
  89.     /** 
  90.      * @return the pool 
  91.      */  
  92.     public ThreadPoolExecutor getPool() {  
  93.         return pool;  
  94.     }  
  95.   
  96.     /** 
  97.      * @param pool 
  98.      *            the pool to set 
  99.      */  
  100.     public void setPool(ThreadPoolExecutor pool) {  
  101.         this.pool = pool;  
  102.     }  
  103.   
  104.     public int getCorePoolSize() {  
  105.         return pool.getCorePoolSize();  
  106.     }  
  107.   
  108.     public void setCorePoolSize(int corePoolSize) {  
  109.         pool.setCorePoolSize(corePoolSize);  
  110.     }  
  111.   
  112.     public int getMaximumPoolSize() {  
  113.         return pool.getMaximumPoolSize();  
  114.     }  
  115.   
  116.     public void setMaximumPoolSize(int maxPoolSize) {  
  117.         pool.setMaximumPoolSize(maxPoolSize);  
  118.     }  
  119.       
  120.   
  121. }  
  1. import java.util.concurrent.Callable;  
  2.   
  3. import mtp.lr.test.base.Base;  
  4.   
  5. public class SubCall implements Callable<String> {  
  6.     Base base = null;  
  7.     String re = "";  
  8.   
  9.     public SubCall(Base base) {  
  10.         // TODO Auto-generated constructor stub  
  11.         this.base = base;  
  12.     }  
  13.   
  14.     @Override  
  15.     public String call() throws Exception {  
  16.         // TODO Auto-generated method stub  
  17.   
  18.         try {  
  19.             re = String.valueOf(base.action());  
  20.         } catch (Throwable e) {  
  21.             // TODO Auto-generated catch block  
  22.             e.printStackTrace();  
  23.         }  
  24.   
  25.         return String.valueOf(re);  
  26.     }  
  27.   
  28. }  

 

  1. public interface Base {  
  2.       
  3.     public int init() throws Throwable;  
  4.       
  5.     public int action() throws Throwable;  
  6.       
  7.     public int end() throws Throwable;  
  8.       
  9. }  

 

  1. import java.util.Random;  
  2.   
  3. import mtp.lr.test.base.Base;  
  4.   
  5.   
  6. public class Test implements Base {  
  7.   
  8.   
  9.     private String TestId = null;  重生之大文豪
  10.   
  11.       
  12.   
  13.     public Test(String TestId) {  
  14.   
  15.   
  16.   
  17.         this.TestId = TestId;  
  18.   
  19.     }  
  20.   
  21.   
  22.     public int init() throws Throwable {  
  23.         return 0;  
  24.     }  
  25.   
  26.     public int action() throws Throwable {  
  27.   
  28.         int StatusCode = 0;  
  29.   
  30.         int resultCode = 0;  
  31.   
  32.         try {  
  33.   
  34.   
  35.         } catch (Throwable t) {  
  36.   
  37.             t.printStackTrace();  
  38.   
  39.   
  40.             return 0;  
  41.         }  
  42.   
  43.         double timer = 0;  
  44.   
  45.         double starttime = 0;  
  46.   
  47.         double overtime = 0;  
  48.   
  49.         try {  
  50.   
  51.             starttime = (double) System.currentTimeMillis();  
  52.             //System.out.println("exec Post");  
  53.             StatusCode = 200;  
  54.             resultCode = 1000;  
  55.             Thread.sleep(new Random().nextInt(10000));  
  56.   
  57.             overtime = (double) System.currentTimeMillis();  
  58.   
  59.               
  60.   
  61.   
  62.         } catch (Throwable t) {  
  63.   
  64.             if (overtime == 0) {  
  65.   
  66.                 overtime = (double) System.currentTimeMillis();  
  67.             }  
  68.   
  69.             t.printStackTrace();  
  70.   
  71.         }  
  72.   
  73.         timer = (overtime - starttime) / 1000;  
  74.   
  75.         if (StatusCode == 200) {  
  76.             if (resultCode == 1000) {  
  77.                 //System.out.println("PASS:"+timer);  
  78.                 return resultCode;  
  79.   
  80.             }  
  81.   
  82.             else {  
  83.   
  84.                 //System.out.println("FAIL:"+timer);  
  85.                 return resultCode;  
  86.             }  
  87.         }  
  88.   
  89.         else {  
  90.   
  91.             //System.out.println("FAIL:"+timer);  
  92.             return resultCode;  
  93.         }  
  94.     }// end of action  
  95.   
  96.       
  97.   
  98.     public int end() throws Throwable {  
  99.         return 0;  
  100.     }// end of end  
  101.   
  102.   
  103.     /** 
  104.      * @return the testId 
  105.      */  
  106.     public String getTestId() {  
  107.         return TestId;  
  108.     }  
  109.   
  110.   
  111.     /** 
  112.      * @param testId the testId to set 
  113.      */  
  114.     public void setTestId(String testId) {  
  115.         TestId = testId;  
  116.     }  
  117.   
  118.       
  119.   
  120. }  

 

  1. import java.util.*;  
  2. import java.util.concurrent.ExecutionException;  
  3.   
  4.   
  5. public class TestThread {  
  6.     public static void main(String[] args) {  
  7.         JavaThreadPool tp = new JavaThreadPool(100);  
  8.       
  9.         for (int i = 0; i < 1000; i++) {  
  10.             String s=String.valueOf(new Random().nextInt(900000) + 99999);  
  11.             Test t = new Test(s);  
  12.             tp.putThread(t,s);  
  13.   
  14.         }  
  15.         while (tp.getResultMap().size() != 0) {  
  16.             System.out.println("TaskCount:" + tp.getPool().getTaskCount());  
  17.             System.out.println("ActiveCount:" + tp.getPool().getActiveCount());  
  18.             System.out.println("CompletedTaskCount:"  
  19.                     + tp.getPool().getCompletedTaskCount());  
  20.             System.out.println("ResultMapSize:" + tp.getResultMap().size());  
  21.             for (int i = 0; i < tp.getResultMap().size(); i++) {  
  22.   
  23.                 if (tp.getTResult(i).isDone()) {  
  24.   
  25.                     try {  
  26.                         System.out.println("TID:" + tp.getTid(i) + ",返回值:"  
  27.                                 + tp.getTResult(i).get());  
  28.                     } catch (InterruptedException e) {  
  29.                         // TODO Auto-generated catch block  
  30.                         e.printStackTrace();  
  31.                     } catch (ExecutionException e) {  
  32.                         // TODO Auto-generated catch block  
  33.                         e.printStackTrace();  
  34.                     }  
  35.                     tp.removeResult(i);  
  36.   
  37.                 }  
  38.                   
  39.             }  
  40.             try {  
  41.                 Thread.sleep(1000);  
  42.             } catch (InterruptedException e) {  
  43.                 // TODO Auto-generated catch block  
  44.                 e.printStackTrace();  
  45.             }  
  46.               
  47.         }  
  48.         tp.shutdown();  
  49.     }  
  50. }  
posted @ 2014-01-07 15:08  将夜  阅读(637)  评论(0编辑  收藏  举报