summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/Listing2203.java
blob: 2d91787c29d5136dd8f8c4bf7d38b54e8925ddef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Listing2203.java */

public class Listing2203
extends Thread
{
  int cnt = 0;

  public void run()
  {
    while (true) {
      if (isInterrupted()) {
        break;
      }
      printLine(++cnt);
    }
  }

  private void printLine(int cnt)
  {
    //Zeile ausgeben
    System.out.print(cnt + ": ");
    for (int i = 0; i < 30; ++i) {
      System.out.print(i == cnt % 30 ? "* " : ". ");
    }
    System.out.println();
    //100 ms. warten
    try {
      Thread.sleep(100);
    } catch (InterruptedException e) {
      interrupt();
    }
  }

  public static void main(String[] args)
  {
    Listing2203 th = new Listing2203();
    {
      //Thread starten
      th.start();
      //2 Sekunden warten
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {
      }
      //Thread unterbrechen
      th.interrupt();
    }
  }
}