What is Python's equivalent to Java's ExecutorService

AsyncTask equivalent in Java

In Android there is the AsyncTask that runs a thread and then I can use the method after it runs to get back to interacting with the main thread. Is there an equivalent to this in Java? I'm trying to run a timer on a separate thread (doInBackground). Once the time is up I can interact with the main theard to restart a service (onPostExecute restarts the service.)

  • Do you mean an ExecutorService, ScheduledExecutorService, Timer?
  • You can just use AsyncTask. Android is open source. grepcode.com/file/repository.grepcode.com/java/ext/…
  • @ Jdavis649 See the Oracle tutorial on executors.
  • Sorry, I should have explained that I am working on a Javafx project, so I cannot use the Asynctask as it is only available on Android

I'm not an Android developer, but I think it could be easily implemented on Java 8 using CompletableFuture:

  • 1 I'm not an expert, but I think you've just reinvented the Executors framework already built into Java.
  • 1 My goal is to provide a class with functionality similar to AsyncTask that will work with the Swings library on Java 8. It can definitely be used as part of the Executors framework as you can define a different execution method (Params ... params, Executor executor)). In addition, similar to onPostExecute, the callback / resume mechanism is not supported in the Executors framework.
  • @TuanDo, why not just use SwingWorker instead?

Solution tailored to you

In short, look at Java: execute a function after a certain number of seconds.

You don't need any for your purpose. is used to do something that takes time but you would prefer not to (such as a complex calculation or getting data from the internet). To work around the problem, you would have to wait.
Instead, you want to introduce a delay, or more precisely, you want to schedule an action to take after a delay. You can use a class like for this as well, but it's overkill and leads to more complicated code. Instead, you should use a class that is tailored for deferred execution and:

Equivalent to in Standard Java

Note that this is associated with a UI concept (UI = user interface) as it may only be started via the UI thread. It is not intended to do anything asynchronously.

Thus the best fitting equivalent to Android is in Java. Swing is the standard UI framework from Java. It has a similar concept to Android with a UI thread. In Swing this thread is called the Event dispatch thread. Hence the design of is also very similar to. Even the method has the same name in both classes.

Asynchronous execution in general

If your request is not related to a user interface and you just want to perform a time-consuming operation to get it to run asynchronously, you need to look at the performers. There are different ways to use executors for many different purposes. This is beyond the scope of this answer. If you are interested in further information, start with Jakob Jenkov's tutorial on ExecutorService.