The Java Program: CurrentThreadDemo.java
1 // CurrentThreadDemo.java -- list known threads (from Handbook)
2
3 class CurrentThreadDemo {
4 public static void main(String args[]) {
5 Thread t = Thread.currentThread();
6 t.setName("My Thread");
7 t.setPriority(1);
8 System.out.println("current thread: " + t);
9 int active = t.activeCount();
10 System.out.println("currently active threads: " + active);
11 Thread all[] = new Thread[active];
12 t.enumerate(all);
13 for (int i = 0; i < active; i++) {
14 System.out.println(i + ": " + all[i]);
15 }
16 try {
17 for (int n = 5; n > 0; n--) {
18 System.out.println("" + n);
19 t.sleep(1000);
20 }
21 } catch (InterruptedException e) {
22 System.out.println("interrupted");
23 }
24 t.dumpStack();
25 }
26 }