Commit 5354648e authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] Creating code that can incorrectly fill a not-thread-safe collection

parent dfc2f745
Loading
Loading
Loading
Loading
+69 −0
Changes for src/main/java/ch15/NotThreadSafeCollection.java: 69 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch15;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NotThreadSafeCollection {
  public static void main(String[] args) {
    ExecutorService threadPool = Executors.newFixedThreadPool(2);
    threadPool.execute(new JobOne());
    threadPool.execute(new JobTwo());
    threadPool.shutdown();
  }
}

class Data {
  private final List<String> letters = new ArrayList<>();
  private final static Data instance = new Data();

  private Data() {
  }

  public static Data getInstance() {
    return instance;
  }

  public List<String> getLetters() {
    return letters;
  }

  public void addLetter(char letter) {
    letters.add(String.valueOf(letter));
  }
}

class JobOne implements Runnable {
  Data a = Data.getInstance();
  char letter = 'A';

  public void run() {
    for (int x = 0; x < 26; x++) {
      a.addLetter(letter++);
      try {
        Thread.sleep(50);
      } catch (InterruptedException ignored) {
      }
    }
    System.out.println("one " + a.getLetters());
    System.out.println("a.getLetters().size() = " + a.getLetters().size());
  }
}

class JobTwo implements Runnable {
  Data a = Data.getInstance();
  char letter = 'a';

  public void run() {
    for (int x = 0; x < 26; x++) {
      a.addLetter(letter++);
      try {
        Thread.sleep(50);
      } catch (InterruptedException ignored) {
      }
    }
    System.out.println("two " + a.getLetters());
    System.out.println("a.getLetters().size() = " + a.getLetters().size());
  }
}