10

I have console application with following threading code. It seems when i hit Ctrl+C to terminate it does not detect control keys, i have to close command prompt window.

Any clues why it is not detecting ctrl+c?

 final ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize); final long SHUTDOWN_TIME = TimeUnit.SECONDS.toMillis(10); for (int i = 0; i < threadPoolSize; i++) { executor.submit(new MessageWorker(topicSubscriber)); } //-- //Add JVM shutdown hook Runtime.getRuntime().addShutdownHook(new Thread() { /** * @see java.lang.Thread#run() */ @Override public void run() { executor.shutdown(); try { if (!executor.awaitTermination(SHUTDOWN_TIME, TimeUnit.SECONDS)) { log.warn("Executor did not terminate in the specified time."); List<Runnable> droppedTasks = executor.shutdownNow(); log.warn("Executor was abruptly shut down. " + droppedTasks.size() + " tasks will not be executed."); } } catch (InterruptedException e) { e.printStackTrace(); } } }); 
7
  • What actually happens? Does program quit but, but hook is not called? Commented Dec 20, 2012 at 6:18
  • program simply does not quit. Commented Dec 20, 2012 at 6:19
  • Do you start it java -jar from command prompt? Commented Dec 20, 2012 at 6:19
  • yes, java -server -jar mypro.jar -Xmx1024m etc... Commented Dec 20, 2012 at 6:20
  • You press Ctrl+C in the same console? It has nothing to do with hook it is something else. Commented Dec 20, 2012 at 6:23

1 Answer 1

8

Just a guess from reading the code, but, it looks like your shutdown time is set as 10,000 seconds, so I'm not surprised you don't think it's quitting!

 final long SHUTDOWN_TIME = TimeUnit.SECONDS.toMillis(10); ... if (!executor.awaitTermination(SHUTDOWN_TIME, TimeUnit.SECONDS)) { 
Sign up to request clarification or add additional context in comments.

2 Comments

wow, so it is possible to postpone termination for such a long time even though Ctrl+C is received?
Most signals (TERM, HUP, CUSTOM, etc) can be blocked completely actually, the danger of doing so is that the caller may choose to send a signal you cannot block. (KILL, etc)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.