ThreadPoolMain.java
资源名称:Source.rar [点击查看]
上传用户:songled
上传日期:2022-07-14
资源大小:94k
文件大小:1k
源码类别:
进程与线程
开发平台:
Java
- public class ThreadPoolMain extends Object {
- public static Runnable makeRunnable(
- final String name,
- final long firstDelay
- ) {
- return new Runnable() {
- public void run() {
- try {
- System.out.println(name +": starting up");
- Thread.sleep(firstDelay);
- System.out.println(name + ": doing some stuff");
- Thread.sleep(2000);
- System.out.println(name + ": leaving");
- } catch ( InterruptedException ix ) {
- System.out.println(name + ": got interrupted!");
- return;
- } catch ( Exception x ) {
- x.printStackTrace();
- }
- }
- public String toString() {
- return name;
- }
- };
- }
- public static void main(String[] args) {
- try {
- ThreadPool pool = new ThreadPool(3);
- Runnable ra = makeRunnable("RA", 3000);
- pool.execute(ra);
- Runnable rb = makeRunnable("RB", 1000);
- pool.execute(rb);
- Runnable rc = makeRunnable("RC", 2000);
- pool.execute(rc);
- Runnable rd = makeRunnable("RD", 60000);
- pool.execute(rd);
- Runnable re = makeRunnable("RE", 1000);
- pool.execute(re);
- pool.stopRequestIdleWorkers();
- Thread.sleep(2000);
- pool.stopRequestIdleWorkers();
- Thread.sleep(5000);
- pool.stopRequestAllWorkers();
- } catch ( InterruptedException ix ) {
- ix.printStackTrace();
- }
- }
- }