使用executor服务的Java异步方法调用

使用executor服务的Java异步方法调用,java,asynchronous,executorservice,Java,Asynchronous,Executorservice,我有一个进行api调用的方法 public Response getPinNumber(int userId){ // I call a method to get the pin (method 1) // Here i have to call another method asynchronously which should not wait for method1 to complete. } 我如何在java中做到这一点?我应该使用executorservice还是线程

我有一个进行api调用的方法

public Response getPinNumber(int userId){
 // I call a method to get the pin (method 1)
 // Here i have to call another method asynchronously which should 
  not wait for method1 to complete. 
}

我如何在java中做到这一点?我应该使用executorservice还是线程?

看起来您需要使用线程。 您需要创建实现可运行接口的类,然后在其重写方法中放置要执行的内容

   class SomeClass implements Runnable{
       @Overriden
       void run(){
       //contents to execute here
       }
    }
然后在方法中创建新线程,如下所示:

Thread thread1 = new Thread(new SomeClass());
然后运行它:

thread1.run();
执行run()后,方法立即返回。
按照本例,您可以创建任意数量的线程。

Executor service更好。@Weibo Li您可以举一个示例,说明如何使用Executor service来创建线程吗?Executor service减少了行数。线程将增加对任何样本的访问??你要求任何样本执行器服务吗?