1. import java.util.concurrent.atomic.AtomicInteger;
  2. public class LogCompactor {
  3. private static final AtomicInteger counter = new AtomicInteger(0);
  4. public static String getLogEntry() {
  5. // Increment the counter for each log entry.
  6. int current = counter.incrementAndGet();
  7. // Format the log entry with the counter.
  8. return "Log Entry " + current;
  9. }
  10. public static void resetCounter() {
  11. // Reset the counter.
  12. counter.set(0);
  13. }
  14. public static void main(String[] args) throws InterruptedException {
  15. // Simulate a scenario where log entries are generated rapidly.
  16. for (int i = 0; i < 10; i++) {
  17. String logEntry = getLogEntry();
  18. System.out.println(logEntry);
  19. Thread.sleep(100); // Simulate some processing time.
  20. }
  21. System.out.println("--- Resetting Log Counter ---");
  22. resetCounter();
  23. for (int i = 0; i < 5; i++) {
  24. String logEntry = getLogEntry();
  25. System.out.println(logEntry);
  26. Thread.sleep(150);
  27. }
  28. }
  29. }

Add your comment