java中的AsyncTask等价物

前端之家收集整理的这篇文章主要介绍了java中的AsyncTask等价物前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

android中有AsyncTask运行一个线程,然后我可以使用post执行的方法再次与主线程交互.在java中有相同的东西吗?我试图在单独的线程(doInBackground)上运行一个计时器,一旦时间结束,它将允许我与主要的theard交互以重新启动服务(onPostExecute将重新启动服务)

最佳答案
我不是Android开发人员,但我认为可以通过在Java 8上使用CompletableFuture轻松实现:

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.CompletableFuture;
import javax.swing.SwingUtilities;

public abstract class AsyncTask CompletableFutureCompletableFuture.supplyAsync( () -> doInBackground(params) );

        // Invoke post execute
        cf.thenAccept(this::onPostExecute);
        return this;
    }
}
原文链接:https://www.f2er.com/java/437285.html

猜你在找的Java相关文章