Larry Raymond
Larry Raymond

Reputation: 1077

What's Java's equivalent of asynchronous callbacks?

Giving the similarity between C# and Java, I expect Java having similar programming support. I heard Java has this Future stuff but I don't know similar it is to C# asynchronous patterns.

Upvotes: 0

Views: 488

Answers (1)

ATrubka
ATrubka

Reputation: 4004

Java basics suggest that for asynchronous processing you have to start a different thread (new or reused). So the simplest code would be as follows:

Thread t = new Thread(new Runnable()) {
    public void run() {
        // your asynchronous code.
    }
});
t.start();

In later versions of java plenty new features were introduced, but they are all based on this simple construct.

For instance, Future and FutureTask mentioned before help facilitate catching result of method invocation performed in a parallel thread. There are many more features in the latest versions of java and particularly in package java.util.concurrent:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/package-summary.html

Upvotes: 4

Related Questions