How to create a Java background thread that allows the main application to exit completely? This works on Linux, but not on Windows

I have a Java application that creates a new thread to do some work. I can start a new thread without problems.

When the "main" program terminates, I want the thread that I created to keep working - what does it do ...

But the problem is that when I launch the main application from Eclipse or from Ant under Windows, control does not return if the background process is not killed.

If I developed the main java process in ant, I want the control to return to Ant when the main thread executes with its work ... But be that as it may, Ant continues to wait until both the main process and the created thread terminate.

How to start a thread in the background so that the control returns to Ant when the main application is finished? (By the way, when I run the same application under Linux, I can do it without problems).

+4
source share
6 answers

It is best to run a completely independent program for this, which is independent of the launching program. You can do this with Runtime#exec() , ProcessBuilder or Desktop#open() .

+4
source

You probably want to create a daemon stream : thread.setDaemon (true).

+2
source

If I understand the question correctly, it seems that the program is behaving correctly ... the main thread ends and ends, and the < background thread continues to work. The JVM will not be completed until all non-daemon threads are completed. If you want the JVM process to terminate when the main thread terminates, you need to do as Roman points out and call Thread.setDaemon(boolean) .

However, if the problem is that the main thread terminates correctly, but background never terminates, even if it completed the task you gave it, background blocked.

The best first IMO step is to start the VisualVM process to dump and use its thread debugging tools to find out what background does and why it hung. (You can also force the JVM to dump the stack by sending it kill -QUIT <pid> if on * nix ... there is something similar to windows with the Break key, but I can't remember the specifics.) The stack scene in Java 6 is enough are complex and will indicate possible deadlocks with objects in which each thread is blocked.

VisualVM is just fun to use, so try it if you have never used it.

+1
source

It looks like you need a "main" thread, and a "background" is a separate process. Scroll through the first process that launches the "main" thread. This process, in turn, deploys the second process, which starts the background thread. On Windows, you may need to start a background process (via the Java Runtime.exec() API) with start /b

I do not understand how this works on Linux. A Process created by Runtime.exec() is either running or not. When a Java program waits for the Process complete, it doesn't matter if the subprocess has one thread or many of them. If you can more clearly describe how you work on Linux, this may help.

0
source

Management will not return from the JVM until everything is complete. This means that all threads should be returned, and all windows should be destroyed () ed - and main should exit.

Or you call System.exit ().

If this really worked on Linux, I am sure you will find that your background thread has also terminated - otherwise it is very bad.

0
source

I got it. As it turns out, on Windows, processes running from the same command window will wait for the child processes to finish.

You can verify this in eclipse: create a program and execute the application (e.g. Notepad.exe). You will find that the java program terminates, but the red button is still active. The control does not return to eclipse until you close the application (for example, notepad.exe). This is apparently due to the fact that your Java program and executable program use the same command window. The same thing happens in ANT.

On linux, processes do not inherit the command window, as on Windows, so control returns to eclipse when your process ends.

In both cases, the generated process remains alive until it is complete.

0
source

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


All Articles