1. create a tread pool manager provide a thread pool.
package com.tian.jack.test; import java.util.Vector; public class ThreadPoolManager { @SuppressWarnings("unused") private int maxThread; @SuppressWarnings("unchecked") public Vector vector; public void setMaxThread(int threadCount) { maxThread = threadCount; } public ThreadPoolManager() {} @SuppressWarnings("unchecked") public ThreadPoolManager(int threadCount) { setMaxThread(threadCount); System.out.println("Starting thread pool..."); vector = new Vector(); for (int i = 1; i <= 10; i++) { SimpleThread thread = new SimpleThread(i); vector.addElement(thread); thread.start(); } } public void process(String argument) { int i; for (i = 0; i < vector.size(); i++) { SimpleThread currentThread = (SimpleThread) vector.elementAt(i); if (!currentThread.isRunning()) { System.out.println("Thread " + (i + 1) + " is processing:" + argument); currentThread.setArgument(argument); currentThread.setRunning(true); return; } } if (i == vector.size()) { System.out.println("pool is full, try in another time."); } } }
Test this pool:
package com.tian.jack.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class TestThreadPool { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); String input; ThreadPoolManager manager = new ThreadPoolManager(10); while ((input = br.readLine()) != null) { manager.process(input); } } catch (IOException e) { } } }
run this test, and input some string, the pool will provide thread from thread pool for you.
2. a retry queue test sample
package com.tian.jack.test; public class _Doing { /** * @param args */ private boolean isSuccess = false; public void _execute() throws Exception { isSuccess = false; _doSomething(isSuccess); } public void _doSomething(boolean isSuccess) throws Exception { if (isSuccess) { System.out.println("Done..."); } else { throw new Exception("do something failure..."); } } public boolean isSuccess() { return isSuccess; } public void setSuccess(boolean isSuccess) { this.isSuccess = isSuccess; } }
---------------------------------------------------------------------- package com.tian.jack.test; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class _RetryQueue extends Thread{ private static final Logger logger = LoggerFactory.getLogger(_RetryQueue.class); private boolean terminal = false; //5s private int retryInterval = 5000; private int retries = 3; private static final Map<String, Integer> queue = new ConcurrentHashMap<String, Integer>(); @Override public void run() { while(!terminal){ System.out.println(""); logger.debug("Thread START..."); if(queue.size() <= 0){ logger.debug("Thread STOP..."); throw new RuntimeException("retry time is over, Thread STOPPED!"); } try{ Thread.sleep(retryInterval); }catch(InterruptedException e){ } for(String str : queue.keySet()){ int numberRetries = queue.get(str); if(numberRetries < retries){ try{ logger.debug("retry times {} ", numberRetries); _Doing _doing = new _Doing(); _doing._execute(); queue.remove(str); logger.debug("retry successfully....!!!"); }catch(Exception e){ queue.put(str, numberRetries+1); } }else{ logger.debug("retry times is over...force STOP it!!!!/n"); queue.remove(str); } } } } public static Map<String, Integer> getQueue() { return queue; } }
-------------------------------------------------
package com.tian.jack.test; public class _Test { /** * @param args */ public static void main(String[] args) { _Doing _doing = new _Doing(); try{ _doing._execute(); }catch(Exception e){ _RetryQueue rq = new _RetryQueue(); _RetryQueue.getQueue().put("sender1", 1); rq.start(); } } }
end...