3

I have created a service using procrun which launches certain jars through reflection. When the service is started it starts a thread and rest of the execution happens in that thread. Then each of the plugin loads its own threads and does the execution in there.

During service stop, I have called the stop method of the plugins. Those methods have returned and whatever thread I have created has been terminated for the plugins. But even after that the following threads are still running.

        INFO: Thread No:0 = Timer-0
        Jan 13, 2016 10:49:58 AM com.test.desktop.SdkMain stop
        INFO: Thread No:1 = WebSocketWorker-14
        Jan 13, 2016 10:49:58 AM com.test.desktop.SdkMain stop
        INFO: Thread No:2 = WebSocketWorker-15
        Jan 13, 2016 10:49:58 AM com.test.desktop.SdkMain stop
        INFO: Thread No:3 = WebSocketWorker-16
        Jan 13, 2016 10:49:58 AM com.test.desktop.SdkMain stop
        INFO: Thread No:4 = WebSocketWorker-17
        Jan 13, 2016 10:49:58 AM com.kube.desktop.KubeSdkMain stop
        INFO: Thread No:5 = WebsocketSelector18
        Jan 13, 2016 10:49:58 AM com.test.desktop.SdkMain stop
        INFO: Thread No:6 = AWT-EventQueue-0
        Jan 13, 2016 10:49:58 AM com.test.desktop.SdkMain stop
        INFO: Thread No:7 = DestroyJavaVM
        Jan 13, 2016 10:49:58 AM com.test.desktop.SdkMain stop
        INFO: Thread No:8 = Thread-11
        Jan 13, 2016 10:49:58 AM com.test.desktop.SdkMain stop

The following is how I printed those threads.

ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
        int noThreads = currentGroup.activeCount();
        Thread[] lstThreads = new Thread[noThreads];
        currentGroup.enumerate(lstThreads);
        for (int i = 0; i < noThreads; i++)
            LOGGER.log(Level.INFO, "Thread No:" + i + " = " + lstThreads[i].getName());

Because of these threads, when I stop the service, it takes forever and then times out. But when I call System.exit(0) the service stops quickly. What should I do to get rid of these threads? When I launch the jars through reflection, are there separate threads created for each plugin? If so could these be them? Please advice.

AnOldSoul
  • 3,565
  • 7
  • 38
  • 90
  • How are you trying to `stop` the threads? By the way, maybe `System.exit(0)` is alright to use in your circumstances. – Scary Wombat Jan 13 '16 at 05:42
  • some plugins don't do a good job cleaning up whatever they created. it's just sloppy programming. – ZhongYu Jan 13 '16 at 05:48
  • I have created the threads using ExecutorService. So I am calling the shutDown method of it. I have also ensured this using a boolean volatile variable. – AnOldSoul Jan 13 '16 at 05:49
  • I would start by seeing what the threads are doing -- get a thread dump using jstack, kill -3, VisualVM or some similar tool. That may tell you what they're stuck on, which will tell you how to un-stick them. Also, how are you creating the executor service? – yshavit Jan 13 '16 at 05:53
  • @yshavit - why do you direct your comment at me? I am not suggesting that the OP use `Thread.stop` – Scary Wombat Jan 13 '16 at 05:55
  • @ScaryWombat Oh, sorry! Misread your comment. – yshavit Jan 13 '16 at 05:56
  • @yshavit ExecutorService execService = Executors.newFixedThreadPool(10); and then execService.execute is how they are created. – AnOldSoul Jan 13 '16 at 06:00
  • Can you also print and check what the threads are doing? `Shutdown` call on the executor is just like an instruction and it will only ensure the executor to not take more tasks and go into the shutdown mode. Did you try `ShutdownNow`, which will interrupt the waiting threads?? After that, did you try `awaitTermination(wait time in millis)`? That can forcefully continue the shutdown process? – Vaspar Jan 13 '16 at 09:00
  • I have managed to get rid of threads 1-5 in my question. How can I get rid of the rest? Those are not threads I have created. – AnOldSoul Jan 13 '16 at 09:23

1 Answers1

0

It looks like the plugins are themself launching threads ("INFO: Thread No:1 = WebSocketWorker-14" -> sockets usually should be put in seperate threads) which will not be shut down if you kill the initiating thread. You'll have to enforce your plugins to kill all threads they started when they get shut down to make sure that they will not leave stuff behind. "some plugins don't do a good job cleaning up whatever they created. it's just sloppy programming." - bayou.io is describing it really good there.

Calling System.exit() will just kill the process meaning it will kill all threads created by the process as well.

The other way would be to manually iterate over all running threads, check if it's the main thread, and if not proceed to kill it. You can get all running threads in an iterable set using

Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

And you can get your currently running Thread using

Thread currentThread = Thread.currentThread();

Still this is the way you would not want to do it, it's more of a way to clean up if plugins decide to leave stuff behind rather than just doing it that way. The plugins themselfes should take care of shutting down the threads when they get disabled but if they don't do that you can use above way to manually clean it up.

Pepich1851
  • 108
  • 7
  • I have managed to get rid of the threads 1-5 in my question above. How do I shut down 0,7 and 8. Those seem to be Java threads that I have no control of. – AnOldSoul Jan 13 '16 at 09:33