How to add feature of “SHUTDOWN” in MultiThreaded

2019-09-03 15:20发布

问题:

I am writing a HTTP THREAD-POOLED WEB SERVER code. I made my code at my best. It works fine also.

But there is one problem, whenever I want to shut down my server, I have to use CTRL + C. But that is a bad way to shut down.

I think a lot, to add shutdown feature in my code. But I am unable to do that.

Please help me to add this feature, give any suggestion to do that, I will definitely code that.

EDIT NO. 1

One method that I think is to make one thread that is only listening STDIN input given by keyboard. Whenever it gets "SHUTDOWN". It calls ThreadPol.shutdown(). This way I can achieve this goal.

Is this a right method ? If yes, please help me to implement this.

回答1:

You can do

ThreadPoolExecuto.shutdownNow()

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.