⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadpoolmain.java

📁 Java Thread Programming (Source
💻 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();
		}
	}
}

⌨️ 快捷键说明

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