Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the ways not to wait for execution of a code block or a method?

What are the correct ways/practice/implementation/strategies (or whatever we call it as) for not to wait for code block/method to finish execution in Java?

Assume the following method:

private void myMethod()
{
     // Some lines of code here
     .....
     .....
     .....

     anotherMethod(); // or this could be a code block

     // Again some lines of code here
     .....
     .....
     .....
}

In this case, I expect myMethod() should not wait for code to finish execution of anotherMethod(). I can also assure here that subsequent lines of code do not depend on anything getting executed within anotherMethod().

like image 524
Gnanam Avatar asked Oct 19 '10 10:10

Gnanam


1 Answers

You can start it in another Thread if there is no dependency .

new Thread(new Runnable(){

      public void run(){
           anotherMethod();
      }  

}).start();
like image 189
jmj Avatar answered Oct 06 '22 03:10

jmj