Commit 076cd064 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] Code magnets - if you collapse them into lambdas they don't behave properly!

parent 7597c866
Loading
Loading
Loading
Loading
+12 −22
Changes for src/main/java/ch15/TestThreads.java: 12 added lines, 22 removed lines.
Original line number Diff line number Diff line
@@ -6,8 +6,18 @@ import java.util.concurrent.Executors;
public class TestThreads {
  public static void main(String[] args) {
    ExecutorService pool = Executors.newFixedThreadPool(2);
    pool.execute(new ThreadOne());
    pool.execute(new ThreadTwo());
    pool.execute(() -> {
      Accum a = Accum.getInstance();
      for (int i = 0; i < 98; i++) {
        a.updateCounter(1000);
      }
    });
    pool.execute(() -> {
      Accum a = Accum.getInstance();
      for (int i = 0; i < 99; i++) {
        a.updateCounter(1);
      }
    });
    pool.shutdown();
    System.out.println(Accum.getInstance().getCount());
  }
@@ -33,23 +43,3 @@ class Accum {
    return counter;
  }
}

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

  public void run() {
    for (int i = 0; i < 98; i++) {
      a.updateCounter(1000);
    }
  }
}

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

  public void run() {
    for (int i = 0; i < 99; i++) {
      a.updateCounter(1);
    }
  }
}
 No newline at end of file