SleepInterrupt.java
资源名称:Source.rar [点击查看]
上传用户:songled
上传日期:2022-07-14
资源大小:94k
文件大小:1k
源码类别:
进程与线程
开发平台:
Java
- 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");
- }
- }