How does one implement a truly asynchronous java thread

Posted by Ritesh M Nayak on Stack Overflow See other posts from Stack Overflow or by Ritesh M Nayak
Published on 2010-04-20T16:08:19Z Indexed on 2010/04/20 16:13 UTC
Read the original article Hit count: 239

I have a function that needs to perfom two operations, one which finishes fast and one which takes a long time to run. I want to be able to delegate the long running operation to a thread and I dont care when the thread finishes, but the threads needs to complete. I implemented this as shown below , but, my secondoperation never gets done as the function exits after the start() call. How I can ensure that the function returns but the second operation thread finishes its execution as well and is not dependent on the parent thread ?

public void someFunction(String data)
{
   smallOperation()
   Blah a = new Blah();
   Thread th = new Thread(a);
   th.Start();
}

class SecondOperation implements Runnable
{
  public void run(){
  // doSomething long running
 }
} 

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading