8
A simple Thread
- public class SimpleThread extends Thread {
- public SimpleThread(String s) {
- super(s);
- }
- public void run() {
- for (int i = 0; i < 10; i++) {
- System.out.println(i + " " + getName() + " - " + Thread.activeCount());
- try {
- sleep((long) (Math.random() * 1000));
- }
- catch (InterruptedException e) {
- }
- }
- System.out.println("DONE! " + getName());
- }
- public static void main(String args[]) {
- new SimpleThread("sleepy").start();
- }
- }
$ javac SimpleThread.java
$ java SimpleThread
0 sleepy - 2
1 sleepy - 2
2 sleepy - 2
3 sleepy - 2
4 sleepy - 2
5 sleepy - 2
6 sleepy - 2
7 sleepy - 2
8 sleepy - 2
9 sleepy - 2
DONE! sleepy
The program starts an execution thread which displays 10 messages with random breaks of approximately one second.
Comments