8

A simple Thread

  1. public class SimpleThread extends Thread {
  2.     public SimpleThread(String s) {
  3.         super(s);
  4.     }
  5.  
  6.     public void run() {
  7.         for (int i = 0; i < 10; i++) {
  8.             System.out.println(i + " " + getName() + " - " + Thread.activeCount());
  9.             try {
  10.                 sleep((long) (Math.random() * 1000));
  11.             }
  12.             catch (InterruptedException e) {
  13.             }
  14.         }
  15.         System.out.println("DONE! " + getName());
  16.     }
  17.  
  18.     public static void main(String args[]) {
  19.         new SimpleThread("sleepy").start();
  20.     }
  21. }
$ 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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].