2

A simple Runnable

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

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].