Commit 0958a5c4 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15b] Using lambdas to reduce code

parent daa6e346
Loading
Loading
Loading
Loading
+6 −30
Changes for src/main/java/ch15/LotsOfReaders.java: 6 added lines, 30 removed lines.
Original line number Diff line number Diff line
@@ -10,45 +10,21 @@ public class LotsOfReaders {
    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("Reader 1", chatHistory));
      executor.execute(new ChatReader("Reader 2", chatHistory));
      final int iteration = i;
      executor.execute(() -> chatHistory.add("Hi there! " + iteration));
      executor.execute(() -> printChatHistory("Reader 1", chatHistory));
      executor.execute(() -> printChatHistory("Reader 2", chatHistory));
    }
    executor.shutdown();
  }
}

class ChatReader implements Runnable {
  private String name;
  private ChatHistory chatHistory;

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

  @Override
  public void run() {
  private static void printChatHistory(String name, ChatHistory chatHistory) {
    for (Chat chat : chatHistory.getHistory()) {
      System.out.println(name + " read: " + 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<>();

@@ -62,7 +38,7 @@ class ChatHistory {
}

class Chat {
  private String message;
  private final String message;

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