1

I have several methods (involving network operations) which take quite a long time. I want to call them asynchronously and check the status from time to time. What's the recommended way to do it in Java?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
user496949
  • 83,087
  • 147
  • 309
  • 426
  • Strong duplicate of http://stackoverflow.com/questions/2423210/recommend-a-better-way-to-turn-synchronous-methods-to-asynchronous-in-java – Saurabh Gokhale Mar 21 '11 at 14:47

4 Answers4

3

If you are using java 5 or above, you can use Executor otherwise you can use threads.

Bombe
  • 81,643
  • 20
  • 123
  • 127
Manoj
  • 5,542
  • 9
  • 54
  • 80
2

Asynchronous execution is done with Thread in Java. If you are new to that begin with Thread and Runnable and try to implement some code with them. When you got the idea switch to java.util.concurrent package.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
2

I would use a FutureTask. You give it the task, you can provide code to be run when the task is over (the protected done method), and you can check it's status with the isDone method. If you're writing a swing application you could also use SwingWorker

Ioan Alexandru Cucu
  • 11,981
  • 6
  • 37
  • 39
1

Take a look at the Future interface and the associated documentation: http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html

David Soroko
  • 8,521
  • 2
  • 39
  • 51