0

I need to run plugins in parallel, I'm trying with multithreading but I have no idea on how to close the thread when the task is gone, or do I even need to close it ? Since it's a program just run, display something and end almost immediatly, so if there are some unused threads in the background it don't change much i guess ?

Here is how it look right now, does it look good ?

ArrayList<Thread> listOfThreads = new ArrayList<>();

pluginsList.forEach( ( plugin ) -> {
        Thread thread = new Thread( () -> plugin.run() );
        thread.start();
        listOfThreads.add( thread );
} );
// closing all threads
listOfThreads.forEach( ( thread ) -> thread.stop() );
SkyNalix
  • 57
  • 7
  • 1
    you might want to `join` on the threads, rather than `stop` them, otherwise not much is going to happen – njzk2 Nov 26 '20 at 17:14
  • 2
    You probably want to use an `ExecutorService` and deal with `Future`s, rather than deal with threads directly. – Andy Turner Nov 26 '20 at 17:15
  • 1
    Does this answer your question? [Do I need to clean up Thread objects in Java?](https://stackoverflow.com/questions/58299070/do-i-need-to-clean-up-thread-objects-in-java) – dbl Nov 26 '20 at 17:17
  • Thread.stop() is deprecated in new versions of java. Refer to https://stackoverflow.com/questions/16504140/thread-stop-deprecated. Aviod using it. – Deepak Gupta Nov 26 '20 at 17:24
  • you need not to clean a thread, it cleans itself just after finishes its work. – Alexei Kaigorodov Nov 28 '20 at 12:24

0 Answers0