summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/ThreadedPrimeNumberTools.java
blob: b1629180cb5d6b2d0039619e1d4e1bb74b7b0662 (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
/* ThreadedPrimeNumberTools.java */

public class ThreadedPrimeNumberTools
extends PrimeNumberTools
implements Runnable
{
  private int arg;
  private int func;

  public void printPrimeFactors(int num)
  {
    execAsynchron(1,num);
  }

  public void printPrime(int cnt)
  {
    execAsynchron(2,cnt);
  }

  public void run()
  {
    if (func == 1) {
      super.printPrimeFactors(arg);
    } else if (func == 2) {
      int result = super.getPrime(arg);
      System.out.println("prime number #"+arg+" is: "+result);
    }
  }

  private void execAsynchron(int func, int arg)
  {
    Thread t = new Thread(this);
    this.func = func;
    this.arg  = arg;
    t.start();
  }
}