class ThreadDemo implements Runnable {
ThreadDemo() {
Thread ct = Thread.currentThread();
Thread t = new Thread(this, "Demo");
System.out.println("curThread: " + ct);
System.out.println("Created: " + t);
t.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
System.out.println("exiting main thread");
}
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println("num=" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("child interrupted");
}
System.out.println("exiting child thread");
}
public static void main(String args[]) {
new ThreadDemo();
}
}
Output:
C:\> java ThreadDemo
curThread: Thread[main, 5, main]
Created: Thread[Demo, 5, main]
num= 5
num= 4
num= 3
exiting main Thread
num= 2
num= 1
exiting child thread
Previous slide | Next slide | Back to first slide | View graphic version |