Commit 6188a610 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] rough benchmark for code magnets example - they're the same because the problem is the sleep

parent 2b1aa9b9
Loading
Loading
Loading
Loading
+1 −0
Changes for src/main/java/ch15/exercise1/AddLowerCaseJob.java: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ class AddLowerCaseJob implements Runnable {
      } catch (InterruptedException ignored) {
      }
    }
    // this can throw a ConcurrentModificationException
    System.out.println(Thread.currentThread().getName() + data.getLetters());
    System.out.println(Thread.currentThread().getName() + " getLetters().size() = " + data.getLetters().size());
  }
+9 −1
Changes for src/main/java/ch15/exercise1/SynchronizedCollection.java: 9 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -4,14 +4,22 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class SynchronizedCollection {
  public static void main(String[] args) {
  //13 959 ms
  public static void main(String[] args) throws InterruptedException {
    ExecutorService threadPool = Executors.newFixedThreadPool(2);
    long start = System.currentTimeMillis();
    for (int i = 0; i < 10; i++) {
      Data data = new DataSynchronized();
      threadPool.execute(new AddLowerCaseJob(data));
      threadPool.execute(new AddUpperCaseJob(data));
    }
    threadPool.shutdown();
    threadPool.awaitTermination(5, TimeUnit.MINUTES);
    long end = System.currentTimeMillis();
    System.out.println("Total time: " + (end - start));
  }
}

+9 −1
Changes for src/main/java/ch15/exercise1/ThreadSafeCollection.java: 9 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -4,14 +4,22 @@ import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadSafeCollection {
  public static void main(String[] args) {
  // 13920 ms
  public static void main(String[] args) throws InterruptedException {
    ExecutorService threadPool = Executors.newFixedThreadPool(2);
    long start = System.currentTimeMillis();
    for (int i = 0; i < 10; i++) {
      Data data = new DataSafe();
      threadPool.execute(new AddUpperCaseJob(data));
      threadPool.execute(new AddLowerCaseJob(data));
    }
    threadPool.shutdown();
    threadPool.awaitTermination(5, TimeUnit.MINUTES);
    long end = System.currentTimeMillis();
    System.out.println("Total time: " + (end - start));
  }
}