sleepinterrupt.java

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

JAVA
35
字号
public class SleepInterrupt extends Object implements Runnable {
	public void run() {
		try {
			System.out.println(
					"in run() - about to sleep for 20 seconds");
			Thread.sleep(20000);
			System.out.println("in run() - woke up");
		} catch ( InterruptedException x ) {
			System.out.println(
					"in run() - interrupted while sleeping");
			return;
		}

		System.out.println("in run() - doing stuff after nap");
		System.out.println("in run() - leaving normally");
	}


	public static void main(String[] args) {
		SleepInterrupt si = new SleepInterrupt();
		Thread t = new Thread(si);
		t.start();

		// Be sure that the new thread gets a chance to 
		// run for a while.
		try { Thread.sleep(2000); } 
		catch ( InterruptedException x ) { }

		System.out.println(
				"in main() - interrupting other thread");
		t.interrupt();
		System.out.println("in main() - leaving");
	}
}

⌨️ 快捷键说明

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