Java多线程调用
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.Callable;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- import java.util.concurrent.ThreadPoolExecutor;
- import mtp.lr.test.base.Base;
- public class JavaThreadPool {
- private ThreadPoolExecutor pool = null;
- private Map<String, Future<String>> resultMap = new HashMap<String, Future<String>>();
- private List<String> list = new ArrayList<String>();
- private long tid = 0;
- private SubCall st = null;
- int callCount=1;
- public JavaThreadPool(int poolMax) {
- pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(poolMax);
- // TODO Auto-generated constructor stub
- }
- public void putThread(Base base) {
- list.add(String.valueOf(tid));
- resultMap.put(String.valueOf(tid), submitThread(base));
- tid++;
- }
- public void putThread(Base base, String tmsg) {
- list.add(String.valueOf(tid) + ":" + tmsg);
- resultMap.put(String.valueOf(tid) + ":" + tmsg, submitThread(base));
- tid++;
- }
- public void putThread(Callable<String> sc) {
- list.add(String.valueOf(tid));
- resultMap.put(String.valueOf(tid), submitThread(sc));
- tid++;
- }
- public void putThread(Callable<String> sc, String tmsg) {
- list.add(String.valueOf(tid) + ":" + tmsg);
- resultMap.put(String.valueOf(tid) + ":" + tmsg, submitThread(sc));
- tid++;
- }
- private Future<String> submitThread(Base base) {
- st = new SubCall(base);
- Future<String> f = (Future<String>) pool.submit(st);
- return f;
- }
- private Future<String> submitThread(Callable<String> sc) {
- Future<String> f = (Future<String>) pool.submit(sc);
- return f;
- }
- public void shutdown() {
- pool.shutdown();
- }
- public void shutdownNow() {
- pool.shutdownNow();
- }
- public Future<String> getTResult(int i) {
- return resultMap.get(this.getTid(i));
- }
- public String getTid(int i) {
- return this.list.get(i);
- }
- public void removeResult(int i) {
- resultMap.remove(list.get(i));
- list.remove(i);
- }
- /**
- * @return the resultMap
- */
- public Map<String, Future<String>> getResultMap() {
- return resultMap;
- }
- /**
- * @return the pool
- */
- public ThreadPoolExecutor getPool() {
- return pool;
- }
- /**
- * @param pool
- * the pool to set
- */
- public void setPool(ThreadPoolExecutor pool) {
- this.pool = pool;
- }
- public int getCorePoolSize() {
- return pool.getCorePoolSize();
- }
- public void setCorePoolSize(int corePoolSize) {
- pool.setCorePoolSize(corePoolSize);
- }
- public int getMaximumPoolSize() {
- return pool.getMaximumPoolSize();
- }
- public void setMaximumPoolSize(int maxPoolSize) {
- pool.setMaximumPoolSize(maxPoolSize);
- }
- }
- import java.util.concurrent.Callable;
- import mtp.lr.test.base.Base;
- public class SubCall implements Callable<String> {
- Base base = null;
- String re = "";
- public SubCall(Base base) {
- // TODO Auto-generated constructor stub
- this.base = base;
- }
- @Override
- public String call() throws Exception {
- // TODO Auto-generated method stub
- try {
- re = String.valueOf(base.action());
- } catch (Throwable e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return String.valueOf(re);
- }
- }
- public interface Base {
- public int init() throws Throwable;
- public int action() throws Throwable;
- public int end() throws Throwable;
- }
- import java.util.Random;
- import mtp.lr.test.base.Base;
- public class Test implements Base {
- private String TestId = null; 重生之大文豪
- public Test(String TestId) {
- this.TestId = TestId;
- }
- public int init() throws Throwable {
- return 0;
- }
- public int action() throws Throwable {
- int StatusCode = 0;
- int resultCode = 0;
- try {
- } catch (Throwable t) {
- t.printStackTrace();
- return 0;
- }
- double timer = 0;
- double starttime = 0;
- double overtime = 0;
- try {
- starttime = (double) System.currentTimeMillis();
- //System.out.println("exec Post");
- StatusCode = 200;
- resultCode = 1000;
- Thread.sleep(new Random().nextInt(10000));
- overtime = (double) System.currentTimeMillis();
- } catch (Throwable t) {
- if (overtime == 0) {
- overtime = (double) System.currentTimeMillis();
- }
- t.printStackTrace();
- }
- timer = (overtime - starttime) / 1000;
- if (StatusCode == 200) {
- if (resultCode == 1000) {
- //System.out.println("PASS:"+timer);
- return resultCode;
- }
- else {
- //System.out.println("FAIL:"+timer);
- return resultCode;
- }
- }
- else {
- //System.out.println("FAIL:"+timer);
- return resultCode;
- }
- }// end of action
- public int end() throws Throwable {
- return 0;
- }// end of end
- /**
- * @return the testId
- */
- public String getTestId() {
- return TestId;
- }
- /**
- * @param testId the testId to set
- */
- public void setTestId(String testId) {
- TestId = testId;
- }
- }
- import java.util.*;
- import java.util.concurrent.ExecutionException;
- public class TestThread {
- public static void main(String[] args) {
- JavaThreadPool tp = new JavaThreadPool(100);
- for (int i = 0; i < 1000; i++) {
- String s=String.valueOf(new Random().nextInt(900000) + 99999);
- Test t = new Test(s);
- tp.putThread(t,s);
- }
- while (tp.getResultMap().size() != 0) {
- System.out.println("TaskCount:" + tp.getPool().getTaskCount());
- System.out.println("ActiveCount:" + tp.getPool().getActiveCount());
- System.out.println("CompletedTaskCount:"
- + tp.getPool().getCompletedTaskCount());
- System.out.println("ResultMapSize:" + tp.getResultMap().size());
- for (int i = 0; i < tp.getResultMap().size(); i++) {
- if (tp.getTResult(i).isDone()) {
- try {
- System.out.println("TID:" + tp.getTid(i) + ",返回值:"
- + tp.getTResult(i).get());
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ExecutionException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- tp.removeResult(i);
- }
- }
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- tp.shutdown();
- }
- }
重生之大文豪 www.dwhao.com