Java: interrupts a thread absolutely necessary

I am new to Java and am using code given by someone. There, at the end of the code, they interrupt the stream if it is not already finished. I measure code time.

The problem is that Java code first issues all the threads and then interrupts at the end. Is interruption required? Can't we wait for all the threads to finish? Or it may just be to skip the interrupt (these threads are executed using the exec exec command and they will end anyway). Here is the relevant code. First, the code for a single thread:

String commandString = "./script.scr "; process = Runtime.getRuntime().exec(commandString); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((lsString = bufferedReader.readLine()) != null) { System.out.println(lsString); } try { process.waitFor(); } 

Now the code of the part that sends these streams:

 public void stopWhenAllTaskFinished() { while(notFinished) {sleep(50);} //notFinished is class variable and somewhere else it will set to false. //now finished. //Interrupt all the threads for (int i=0; i<nThreads; i++) { threads[i].interrupt(); } } 

This function is called from the main class, for example:

  obj.stopWhenAllTaskFinished() 

I really appreciate any understanding or answer.

+6
source share
2 answers

It is unclear whether interrupts are needed from the code you posted.

  • If notFinished set to false when all threads are finished, then interrupts are not needed.

  • If notFinished set to false when it is likely that some threads are not finished yet, then this may be necessary. Actually it depends on other things:

    • Are you ready for the application to wait until the threads end naturally?
    • Can you be sure that the flows will end? (Can they hang / lock forever?)
    • Do you need the results of their activities?
    • Will they respond to interruption? (In this case, I think the answer is yes.)
    • Is there any harm if you leave the threads there? (For example, does the application stop closing?)
+1
source

The following code that Oracle provides in javadocs is what most people do. AFAIK, this is for destroying threads that refuse to shut off gracefully after allowing them to shut down.

http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html

  void shutdownAndAwaitTermination(ExecutorService pool) { pool.shutdown(); // Disable new tasks from being submitted try { // Wait a while for existing tasks to terminate if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { pool.shutdownNow(); // Cancel currently executing tasks // Wait a while for tasks to respond to being cancelled if (!pool.awaitTermination(60, TimeUnit.SECONDS)) System.err.println("Pool did not terminate"); } } catch (InterruptedException ie) { // (Re-)Cancel if current thread also interrupted pool.shutdownNow(); // Preserve interrupt status Thread.currentThread().interrupt(); } } 
+2
source

Source: https://habr.com/ru/post/914686/


All Articles