import java.util.concurrent.atomic.AtomicInteger;
public class LogCompactor {
private static final AtomicInteger counter = new AtomicInteger(0);
public static String getLogEntry() {
// Increment the counter for each log entry.
int current = counter.incrementAndGet();
// Format the log entry with the counter.
return "Log Entry " + current;
}
public static void resetCounter() {
// Reset the counter.
counter.set(0);
}
public static void main(String[] args) throws InterruptedException {
// Simulate a scenario where log entries are generated rapidly.
for (int i = 0; i < 10; i++) {
String logEntry = getLogEntry();
System.out.println(logEntry);
Thread.sleep(100); // Simulate some processing time.
}
System.out.println("--- Resetting Log Counter ---");
resetCounter();
for (int i = 0; i < 5; i++) {
String logEntry = getLogEntry();
System.out.println(logEntry);
Thread.sleep(150);
}
}
}
Add your comment