Thread sample code
来源:互联网 发布:苹果手机制作铃声软件 编辑:程序博客网 时间:2024/06/12 15:36
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...
- Thread sample code
- Java Thread Local – How to use and code sample
- sample code
- JDK 1.5 Sample Code
- Events Sample Code(1)
- Events Sample Code(2)
- logic font sample code
- MetaWeblogAPI C# Code Sample
- JavaScript Sample Code
- XMl - xerces code sample
- java concurrent code sample
- Sample CRC Code
- sample code of webview
- Spring AOP Sample Code
- sample transaction code
- File Output sample code
- MKMapView Sample Code
- !!!C++ sample code
- Ubuntu下为QT编译mysql驱动
- C# 多线程与异步操作实现的探讨
- 什么才是真正的云计算的PaaS?
- how to get callstack at runtime?
- USB2.O设备PCB设计要点
- Thread sample code
- mysql字段分组后,某个字段组成一个多值字段
- AlertDialog的样式修改
- 索引监控
- 对于 epoll ,转的
- SynchronizedMap和ConcurrentHashMap的深入分析
- C++读取文本文件
- javascript实现的日历
- 使用NUnit进行DotNet程序测试