Commit b0b1a2fe authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] 2nd Ed Code magnets

parent 4d0ea73b
Loading
Loading
Loading
Loading
+62 −0
Changes for src/main/java/ch15/TestThreads.java: 62 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch15;

public class TestThreads {
  public static void main(String[] args) {
    ThreadOne t1 = new ThreadOne();
    ThreadTwo t2 = new ThreadTwo();
    Thread one = new Thread(t1);
    Thread two = new Thread(t2);
    one.start();
    two.start();
  }
}

class Accum {
  private static Accum a = new Accum();
  private int counter = 0;

  private Accum() {
  }

  public static Accum getAccum() {
    return a;
  }

  public void updateCounter(int add) {
    counter += add;
  }

  public int getCount() {
    return counter;
  }
}

class ThreadOne implements Runnable {
  Accum a = Accum.getAccum();

  public void run() {
    for (int x = 0; x < 98; x++) {
      a.updateCounter(1000);
      try {
        Thread.sleep(50);
      } catch (InterruptedException ex) {
      }
    }
    System.out.println("one " + a.getCount());
  }
}

class ThreadTwo implements Runnable {
  Accum a = Accum.getAccum();

  public void run() {
    for (int x = 0; x < 99; x++) {
      a.updateCounter(1);
      try {
        Thread.sleep(50);
      } catch (InterruptedException ex) {
      }
    }
    System.out.println("two " + a.getCount());
  }
}
 No newline at end of file