This is Java. Move your processing to a second thread. Now you can
- Read from stdin in a loop. If someone types "QUIT", set the while flag to false and exit.
- Create a AWT or Swing frame with a STOP button.
- Pretend you are a Unix daemon and create a server socket. Wait for someone to open the socket and send "QUIT". (This has the added bonus that you can change the sleep to a select with timeout.)
There must be hundreds of variants on this.