简体   繁体   English

异步任务替换

[英]Async Task Replacement

What is the replacement of async task as it is deprecated and how to use the replacement?什么是异步任务的替代品,因为它已被弃用以及如何使用替代品? any suggestions.有什么建议么。

Replacement that functions same as async task.与异步任务功能相同的替换。

Using the AsyncTask API might allow you to execute asynchronous tasks, but there are issues in plain sight:使用 AsyncTask API 可能允许您执行异步任务,但存在一些显而易见的问题:

  1. The set up is too cumbersome for simple tasks设置对于简单的任务来说太麻烦了
  2. The API is prone to memory leaks API容易出现内存泄漏
  3. The API has been deprecated as of API level 30从 API 级别 30 开始,该 API 已被弃用

Alternatives to the deprecated AsyncTask已弃用的 AsyncTask 的替代品

Due to AsyncTask's shortcomings and deprecation, developers should turn to one of these alternative solutions for asynchronous programming in Android:由于 AsyncTask 的缺点和弃用,开发人员应该转向以下替代解决方案之一进行 Android 中的异步编程:

  • Kotlin coroutines Kotlin 协程
  • RxJava RxJava
  • Executors执行者

The first two are more popular and have simpler API usages前两个更受欢迎,API 用法更简单

You can directly use Executors from java.util.concurrent package.您可以直接使用 java.util.concurrent 包中的执行器。

I also searched about it and I found a solution in this Android Async API is Deprecated post.我还搜索了它,并在这个 Android Async API is Deprecated 帖子中找到了解决方案。

Unfortunately, the post is using Kotlin, but after a little effort I have converted it into Java.不幸的是,该帖子使用的是 Kotlin,但经过一些努力我将其转换为 Java。 So here is the solution.所以这是解决方案。

ExecutorService executor = Executors.newSingleThreadExecutor();
Handler handler = new Handler(Looper.getMainLooper());

executor.execute(new Runnable() {
    @Override
    public void run() {

        //Background work here

        handler.post(new Runnable() {
            @Override
            public void run() {
                //UI Thread work here
            }
        });
    }
});

Pretty simple right?很简单吧? You can simplify it little more if you are using Java 8 in your project.如果您在项目中使用 Java 8,则可以稍微简化它。

ExecutorService executor = Executors.newSingleThreadExecutor();
Handler handler = new Handler(Looper.getMainLooper());

executor.execute(() -> {
    //Background work here
    handler.post(() -> {
        //UI Thread work here
    });
});

Still, it cannot defeat kotlin terms of conciseness of the code, but better than the previous java version.尽管如此,它在代码的简洁性方面仍然无法击败kotlin,但比以前的java版本要好。

Hope this will help you.希望这会帮助你。 Thank You谢谢你

AsyncTask allows you to perform background operations and publish results on the UI thread. AsyncTask 允许您执行后台操作并在 UI 线程上发布结果。

You can use the below alternatives:您可以使用以下替代方案:

WorkManager : You can use WorkManager, a part of the Android Jetpack library, to schedule and manage background tasks. WorkManager :您可以使用 WorkManager(Android Jetpack 库的一部分)来安排和管理后台任务。

Coroutines : You can use the CoroutineScope class and related classes in the kotlinx.coroutines package to perform background operations. Coroutines :您可以使用 kotlinx.coroutines 包中的 CoroutineScope 类和相关类来执行后台操作。 Coroutines are a lightweight threading option that allow you to write asynchronous code.协程是一种轻量级线程选项,可让您编写异步代码。

Executor framework: You can use the Executor class and related classes in the java.util.concurrent package to perform background operations. Executor框架:可以使用java.util.concurrent包中的Executor类及相关类进行后台操作。 This is a more powerful option that allows you to specify thread pools and customize the execution of your tasks.这是一个更强大的选项,允许您指定线程池并自定义任务的执行。

Threads : You can use the Thread class to perform background operations. Threads :您可以使用 Thread 类来执行后台操作。 This is a low-level option that requires you to handle thread management and communication with the main thread yourself.这是一个低级选项,需要您自己处理线程管理和与主线程的通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM