Java多线程调用

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import mtp.lr.test.base.Base;

public class JavaThreadPool {
	//线程池
	private ThreadPoolExecutor pool = null;
	//结果集
	private Map<String, Future> resultMap = new HashMap<String, Future>();
	//结果集对应的list,用于映射结果集和自定义线程id
	private List list = new ArrayList();
	//自定义线程id
	private long tid = 0;
	//子线程
	private SubCall st = null;
	//同步锁
	ReentrantLock MapLock = new ReentrantLock();

	public JavaThreadPool(int coreSize, int maxSize, int queueSize) {
		//设置线程池
		pool = new ThreadPoolExecutor(coreSize, maxSize, 0L,
				TimeUnit.MILLISECONDS, new LinkedBlockingQueue(
						queueSize));
		//如果超出队列则会抛出异常
		pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());

		// TODO Auto-generated constructor stub
	}
	//把子线程submit到线程池的队列
	public void putThread(Base base) throws RejectedExecutionException {
		MapLock.lock();
		list.add(String.valueOf(tid));
		resultMap.put(String.valueOf(tid), submitThread(base));
		tid++;
		MapLock.unlock();
		
	}

	public void putThread(Base base, String tmsg)
			throws RejectedExecutionException, OutOfMemoryError {
		MapLock.lock();
		list.add(String.valueOf(tid) + ":" + tmsg);
		resultMap.put(String.valueOf(tid) + ":" + tmsg, submitThread(base));
		tid++;
		MapLock.unlock();
		
	}

	public void putThread(Callable sc)
			throws RejectedExecutionException {
		MapLock.lock();
		list.add(String.valueOf(tid));
		resultMap.put(String.valueOf(tid), submitThread(sc));
		tid++;
		MapLock.unlock();
		
	}

	public void putThread(Callable sc, String tmsg)
			throws RejectedExecutionException {

		MapLock.lock();
		list.add(String.valueOf(tid) + ":" + tmsg);
		resultMap.put(String.valueOf(tid) + ":" + tmsg, submitThread(sc));
		tid++;
		MapLock.unlock();
	}
	//提交子线程到线程池队列,获取异步结果
	private Future submitThread(Base base) {
		st = new SubCall(base);
		Future f = (Future) pool.submit(st);
		return f;
	}

	private Future submitThread(Callable sc) {
		Future f = (Future) pool.submit(sc);
		return f;
	}

	public void shutdown() {
		pool.shutdown();
	}

	public void shutdownNow() {
		pool.shutdownNow();
	}

	public void awaitTermination(long time, TimeUnit timeunit) {
		try {
			pool.awaitTermination(time, timeunit);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			pool.shutdownNow();
		}
	}
	//从结果集中获取一个结果
	public Future getTResult(int i) {
		MapLock.lock();
		Future f = resultMap.get(this.getTid(i));
		MapLock.unlock();
		return f;
	}
	//获取结果集第i个结果的tid
	public String getTid(int i) {
		MapLock.lock();
		String s = this.list.get(i);
		MapLock.unlock();
		return s;
	}
	//移除结果
	public void removeResult(int i) {
		MapLock.lock();
		resultMap.remove(list.get(i));
		list.remove(i);
		MapLock.unlock();
	}

	/**
	 * @return the resultMap
	 */
	public Map<String, Future> getResultMap() {
		return resultMap;
	}

	public int getResultMapSize() {
		MapLock.lock();
		int size = resultMap.size();
		MapLock.unlock();
		return size;
	}

	/**
	 * @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);
	}

	public long getTaskCount() {
		long l = pool.getTaskCount();
		return l;
	}

	public int getActiveCount() {
		return pool.getActiveCount();
	}

	public long getCompletedTaskCount() {
		return pool.getCompletedTaskCount();
	}

	public BlockingQueue getQueue() {
		return pool.getQueue();
	}

	public ReentrantLock getMapLock() {
		return MapLock;
	}

}

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) {
		//设置线程池大小,线程池最大大小,等待提交任务队列最大长度
		final JavaThreadPool tp = new JavaThreadPool(2000, 10000,
				Integer.MAX_VALUE);
		//所有任务数
		final long allTaskCount = Long.MAX_VALUE;
		//用于控制待提交任务数
		final int taskCount = 50000;
		//提交任务线程,和处理任务结果线程分开
		new Thread() {
			public void run() {
				//不停新增任务直至allTaskCount
				for (long i = 0; i <allTaskCount; i++) {
					String s = String
							.valueOf(new Random().nextInt(900000) + 99999);
					Test t = new Test(s);
					//待处理的结果集超出限制时暂停向队列新增任务
					//getResultMapSize 处理中数据集数量
					if (tp.getResultMapSize() < 10000) {
						//限制队列任务数,Queue未处理任务数
						if (tp.getQueue().size() < taskCount) {
							try {
								//提交任务到队列
								tp.putThread(t, s);
							} catch (Exception e) {
								e.printStackTrace();
							}
						} else {
							//队列超限,暂停一下
							System.out.println("tasks out of limit!");
							try {
								sleep(10);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
						}

					}
					else{
						//结果集超限,暂停一下
						System.out.println("ResultMapSize out of limit ! please wait sometime and don't put subimt~~");
						try {
							sleep(50);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				//终于提交完了?
				System.out.println("tasks put over, count:!"
						+ tp.getTaskCount());
			}
		}.start();
		//处理任务结果线程
		new Thread() {
			public void run() {
				//计时计数
				long ocount = 0;
				long ncount = 0;
				double stime = (double) System.currentTimeMillis();
				double ttime;
				//循环处理任务集直至allTaskCount
				while (tp.getCompletedTaskCount() != allTaskCount) {
					ttime = ((double) System.currentTimeMillis() - stime) / 1000;
					stime = (double) System.currentTimeMillis();
					ocount = tp.getCompletedTaskCount() - ncount;
					ncount = tp.getCompletedTaskCount();
					System.out.println("Time:" + ttime);
					System.out.println("exeCount:" + ocount);
					System.out.println("LastTps:" + ocount / ttime);
					System.out.println("TaskCount:" + tp.getTaskCount());
					System.out.println("CompletedTaskCount:"
							+ tp.getCompletedTaskCount());
					System.out.println("QueueSize:" + tp.getQueue().size());
					System.out.println("ResultMapSize:"
							+ tp.getResultMap().size());
					//处理全部结果集
					for (int i = 0; i < tp.getResultMapSize(); i++) {
						//已经完成的结果集就处理后移除
						if (null != tp.getTResult(i)
								&& tp.getTResult(i).isDone()) {

							try {
								//do something
								//获得处理结果
								String s = "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);

						} 
						//没有找到对应的结果集,跳出重新开始大循环
						else {
							break;
						}

					}
				}
				System.out.println("Over CompletedTaskCount:"
						+ tp.getCompletedTaskCount());
				System.out.println("STOP!");
				tp.shutdown();

			}
		}.start();
		
	}
}






posted @ 2014-01-07 14:57  zymaxs  阅读(214)  评论(0编辑  收藏  举报