threadpoolmain.java

来自「Java Thread Programming (Source」· Java 代码 · 共 60 行

JAVA
60
字号
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();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?