1. import java.util.Scanner;
  2. import java.util.concurrent.atomic.AtomicBoolean;
  3. public class QueueMonitor {
  4. private static final Scanner scanner = new Scanner(System.in);
  5. private static AtomicBoolean running = true;
  6. public static void main(String[] args) {
  7. // Start the monitoring loop
  8. monitorQueue();
  9. }
  10. private static void monitorQueue() {
  11. while (running) {
  12. System.out.println("Press Ctrl+C to stop.");
  13. try {
  14. scanner.nextInt(); // Wait for user input to prevent busy-waiting
  15. } catch (Exception e) {
  16. if (e instanceof java.util.hasNextException) {
  17. // User pressed Ctrl+C
  18. running = false;
  19. }
  20. }
  21. // Simulate checking the queue (replace with actual queue polling logic)
  22. checkQueue();
  23. }
  24. System.out.println("Monitoring stopped.");
  25. }
  26. private static void checkQueue() {
  27. // Replace this with your actual queue checking logic.
  28. // This is a placeholder.
  29. System.out.println("Checking queue...");
  30. // Simulate queue processing
  31. try {
  32. Thread.sleep(1000); // Simulate a 1-second check interval
  33. } catch (InterruptedException e) {
  34. Thread.currentThread().interrupt();
  35. }
  36. System.out.println("Queue check complete.");
  37. }
  38. }

Add your comment