I use the proprietary library.

In the main() class, I create an instance of the Thread-safe configurated class from this library.

Then, in multitrading, using this single instance, I run methods of this class with different parameters.

I join() each created thread. At the end of these methods and the main () method, I have the necessary result.

However, even after the end of the main() method, the cursor continues to blink on the command line, indicating that the program continues to run.

I believe that these are streams launched by methods of this class (although the methods themselves have completed the work!).

How can I programmatically terminate these threads? I don’t have access to the sources, I cannot refuse this library.

  • since the library is unknown, I recommend reading the documentation for it and asking the developers of this library. If this does not help, then look at the debugger / profiler / disassembler and figure it out. If this does not help, write the library yourself. In case if after this the solution is not found - go to another company. - KoVadim
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

A quick reference to threads in Java. Threads are of two types:

  • Daemon (daemon) - service flow
  • Main thread (non-demon)

The JVM exits the program when all its non-daemon threads are terminated. As soon as the last non-demon thread finishes, all the demons are immediately destroyed. Therefore, to ensure that all threads are completed after the completion of your main thread, they need to be demons. And here is a handy thing: a daemon thread can only spawn demons. Therefore, you need to create a new thread in Maine, make it a demon, and launch third-party libraries in it. When your main thread runs out, all the demons will end right there. An example of how I did when I encountered the same problem:

 Thread daemon = new Thread() { @Override public void run() { bot.init(log); //в этом методе запускаются потоки сторонних библиотек } }; daemon.setDaemon(true); //делаем поток демоном daemon.start(); //запускаем. Все потоки, порожденные внутри него, //будут тоже демонами, и не будут мешать завершению программы 

It is only necessary to take into account that as soon as your main finished, all daemons will be completed (if there are no other non-demons). I advise you to think thoroughly about how best to divide the threads into main and service ones, in case of emergency you can wait for the daemon to complete using join() known to you.