How does one implement a truly asynchronous java thread
The JVM will not exit before the thread terminates. This code that you posted does not even compile; perhaps the problem is in your actual code.
IF your second function is not getting done it has nothing to do with your function returning. If something calls System.exit()
or if your function throws an exception, then the thread will stop. Otherwise, it will run until it is complete, even if your main thread stops. That can be prevented by setting the new thread to be a daemon, but you are not doing that here.
public void someFunction(final String data) {
shortOperation(data);
new Thread(new Runnable() {
public void run(){
longOperation(data);
}
}).start();
}
If someFunction
is called, the JVM will run the longOperation
if
- the thread running it is not marked as a daemon (in the above code it is not)
- the
longOperation()
does not throw an exception and - no calls to System.exit() is made in
longOperation()