Asynchronous Method Invocation in Java

I’m working on a web application that needs to kick off a process that could take anywhere from 5 seconds to an hour, so I need to be able to call a method and have it return immediately. A google search for ‘java asynchronous method‘ didn’t return much, except this, which seems to indicate that simply spawning a new thread within a method will do the job. Basically you end up with something like this:

public class LongProcess implements Runnable {
  public void startProcess() {
    Thread t = new Thread(this);
    t.start();
  }
  
  public void run() {
    // perform the long running process
  }
}

and then you can kick the process off like this:

LongProcess p = new LongProcess();
p.startProcess();

The other solution I thought of would be to use an asychronous xml-rpc call, although this obviously has more overhead.

Are there any solutions I’m missing? What would you do?

13 thoughts on “Asynchronous Method Invocation in Java”

  1. I think Matt might’ve meant this:

    new Thread(new Runnable() {
      public void run() {
        // do it…
      }
    }).start();

    If you were in an EJB container, technically you are really not supposed to launch your own threads. It violates the ability of the container to manage threads for you (pooling, maintaining thread-local state, and whatnot). Instead you’re supposed to use JMS and MDB’s, I think.

    Not sure what the rule is for servlet containers though. If the spec doesn’t prohibit you from creating a new thread, do so. Just be careful that any data structures that need to be threadsafe, are, and that you don’t call this code so many times that your app could create a very large number of threads.

  2. Thanks Aapo. I thought of JMS as well, I don’t have any experience using it, but it would certainly fit the bill of allowing asynchronous methods to be called… Wouldn’t it be overkill though? JMS is an entirely different piece of middleware that I would then have to configure and manage… all to call a single method.

    I’ve used a database before in situations like this. Insert a row, have some other process polling for the existence of rows… but that means you have to have access to some kind of scheduling/cron application and it assumes the existence of a database and/or file system.

  3. Jason.. a thread pool huh? Like this one:

    http://jakarta.apache.org/commons/sandbox/threadpool/

    That does look nice. From the description:

    Commons ThreadPool is a component for working with pools of threads and asynchronously executing tasks. Often inside application servers and containers the threading model is managed for you. However in some circumstances it can be useful to use your own worker thread pools to dispatch asynchronous work into. In enterprise class systems typically JMS is used to perform some task asynchronously by another machine. However ThreadPools can be a simple and effective mechanism for asynchronous processing within the same process. This can be particularly useful for performing asynchronous processing in Servlet engines or Swing applications.

  4. You may want to use a simple, queue based solution. Basically, construct a command object that encapsulates the long running process and put it in a queue. Then use a manager to pop these commands off the queue and execute them using 1 or more threads. This way you can actually control how many threads are being used depending on number of tasks in the queue. Also the added benefit is simpler request handler code.

  5. Hi,

    I am facing the same problem. Can you share your finest experiences of which was better. infact im using weblogic 8.1 sp5. please post me which is a better approach for asynchronous processing in java. Other than threads(preferably) or with threads which is the best option.
    Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>