How does one implement a truly asynchronous java thread How does one implement a truly asynchronous java thread multithreading multithreading

How does one implement a truly asynchronous java thread


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

  1. the thread running it is not markedas a daemon (in the above code itis not)
  2. the longOperation() does not throw an exception and
  3. no calls to System.exit() is made in longOperation()


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.