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

[Ch15b] Now trying to create the simplest example that throws concurrent modification

parent 2539061f
Loading
Loading
Loading
Loading
+74 −0
Changes for src/main/java/ch15/LotsOfReaders.java: 74 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 LotsOfReaders {
  public static void main(String[] args) {
    ChatHistory chatHistory = new ChatHistory();
    ExecutorService executor = Executors.newFixedThreadPool(4);
    for (int i = 0; i < 5; i++) {
      executor.execute(new Chatty(chatHistory));
      executor.execute(new ChatReader(chatHistory));
      executor.execute(new ChatReader(chatHistory));
    }
    executor.shutdown();
  }

}

class ChatReader implements Runnable {
  private ChatHistory chatHistory;

  public ChatReader(ChatHistory chatHistory) {
    this.chatHistory = chatHistory;
  }

  @Override
  public void run() {
    for (Chat chat : chatHistory.getHistory()) {
      System.out.println(chat);
    }
  }
}


class Chatty implements Runnable {
  private ChatHistory chatHistory;

  public Chatty(ChatHistory chatHistory) {
    this.chatHistory = chatHistory;
  }

  @Override
  public void run() {
    chatHistory.add("Hi there!");
  }
}

class ChatHistory {
  private final List<Chat> history = new ArrayList<>();

  public List<Chat> getHistory() {
    return history;
  }

  public void add(String message) {
    history.add(new Chat(message));
  }
}

class Chat {
  private String message;

  public Chat(String message) {
    this.message = message;
  }

  @Override
  public String toString() {
    return message;
  }
}