谁能给个线程池应用的例子程序 谢谢了谢谢
解决方案 »
- 做通信软件需要什么技术,大家发表一下意见呵呵
- struts2.0 验证应用问题
- Java经典好书
- 求助:什么是数据源?如何配置??
- 哭求高手指点在struts中使用paramId、paramName、paramProperty传递中文参数出现乱码的解决
- 大侠们帮忙看看这个递归程序出了什么问题?
- 哪位大侠在真实GPRS网络里成功实现UDP和Socket连接,请说一下配置好吗?
- 在tomcat下编写的JSP程序在WebSphere下也能成功运行吗?
- 关于struts实现翻页功能我这里有参考的元代吗,但是不能实现
- 加分!(在线等待)JBuilder7 和 WebLogic7 可不可以装在不同的机器上开发
- 刚毕业不久,求ssh框架项目,学习
- eclipse插件开发如何定制自己的菜单
if (args.length != 2) {
System.out.println("Tests the ThreadPool task.");
System.out
.println("Usage: java ThreadPoolTest numTasks numThreads");
System.out.println(" numTasks - integer: number of task to run.");
System.out.println(" numThreads - integer: number of threads "
+ "in the thread pool.");
return;
}
int numTasks = Integer.parseInt(args[0]);
int numThreads = Integer.parseInt(args[1]); // create the thread pool
ThreadPool threadPool = new ThreadPool(numThreads); // run example tasks
for (int i = 0; i < numTasks; i++) {
threadPool.runTask(createTask(i));
} // close the pool and wait for all tasks to finish.
threadPool.join();
} /**
* Creates a simple Runnable that prints an ID, waits 500 milliseconds, then
* prints the ID again.
*/
private static Runnable createTask(final int taskID) {
return new Runnable() {
public void run() {
System.out.println("Task " + taskID + ": start"); // simulate a long-running task
try {
int i = 0;
while (i<99999L*2000)
i++;
} catch (Exception ex) {
} System.out.println("Task " + taskID + ": end");
}
};
} }
/**
* A thread pool is a group of a limited number of threads that are used to
* execute tasks.
*/ class ThreadPool extends ThreadGroup { private boolean isAlive; private LinkedList taskQueue; private int threadID; private static int threadPoolID; /**
* Creates a new ThreadPool.
*
* @param numThreads
* The number of threads in the pool.
*/
public ThreadPool(int numThreads) {
super("Zigzag_ThreadPool-" + (threadPoolID++));
setDaemon(true); isAlive = true; taskQueue = new LinkedList();
for (int i = 0; i < numThreads; i++) {
new PooledThread().start();
}
} /**
* Requests a new task to run. This method returns immediately, and the task
* executes on the next available idle thread in this ThreadPool.
* <p>
* Tasks start execution in the order they are received.
*
* @param task
* The task to run. If null, no action is taken.
* @throws IllegalStateException
* if this ThreadPool is already closed.
*/
public synchronized void runTask(Runnable task) {
if (!isAlive) {
throw new IllegalStateException();
}
if (task != null) {
taskQueue.add(task);
notify();
} } protected synchronized Runnable getTask() throws InterruptedException {
while (taskQueue.size() == 0) {
if (!isAlive) {
return null;
}
wait();
}
return (Runnable) taskQueue.removeFirst();
} /**
* Closes this ThreadPool and returns immediately. All threads are stopped,
* and any waiting tasks are not executed. Once a ThreadPool is closed, no
* more tasks can be run on this ThreadPool.
*/
public synchronized void close() {
if (isAlive) {
isAlive = false;
taskQueue.clear();
interrupt();
}
} /**
* Closes this ThreadPool and waits for all running threads to finish. Any
* waiting tasks are executed.
*/
public void join() {
// notify all waiting threads that this ThreadPool is no
// longer alive
synchronized (this) {
isAlive = false;
notifyAll();
} // wait for all threads to finish
Thread[] threads = new Thread[activeCount()];
int count = enumerate(threads);
for (int i = 0; i < count; i++) {
try {
threads[i].join();
} catch (InterruptedException ex) {
}
}
} /**
* A PooledThread is a Thread in a ThreadPool group, designed to run tasks
* (Runnables).
*/
private class PooledThread extends Thread { public PooledThread() {
super(ThreadPool.this, "Zigzag_ThreadPool-" + (threadID++));
} public void run() {
while (!isInterrupted()) { // get a task to run
Runnable task = null;
try {
task = getTask();
} catch (InterruptedException ex) {
} // if getTask() returned null or was interrupted,
// close this thread by returning.
if (task == null) {
return;
} // run the task, and eat any exceptions it throws
try {
task.run();
} catch (Throwable t) {
uncaughtException(this, t);
}
}
}
}
}
* start method
*/
public void init() {
for (int i = 0; i < ThreadSize; i++) {
SnmpThread sp = new SnmpThread("你要初始化的参数");
pool.execute(sp);
}
} public void stop() {
pool.shutdown();
}