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

[Ch15b] Adding timestamp, removing iteration count

parent 0958a5c4
Loading
Loading
Loading
Loading
+10 −4
Changes for src/main/java/ch15/LotsOfReaders.java: 10 added lines, 4 removed lines.
Original line number Diff line number Diff line
package ch15;

import java.time.LocalDateTime;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static java.time.format.DateTimeFormatter.ofLocalizedTime;

public class LotsOfReaders {
  public static void main(String[] args) {
    ChatHistory chatHistory = new ChatHistory();
    ExecutorService executor = Executors.newFixedThreadPool(4);
    ExecutorService executor = Executors.newFixedThreadPool(3);
    for (int i = 0; i < 5; i++) {
      final int iteration = i;
      executor.execute(() -> chatHistory.add("Hi there! " + iteration));
      executor.execute(() -> chatHistory.add("Hi there!"));
      executor.execute(() -> printChatHistory("Reader 1", chatHistory));
      executor.execute(() -> printChatHistory("Reader 2", chatHistory));
    }
@@ -39,13 +42,16 @@ class ChatHistory {

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

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

  @Override
  public String toString() {
    return message;
    String time = timestamp.format(ofLocalizedTime(FormatStyle.MEDIUM));
    return time + " " + message;
  }
}