如何在 android 中实现启动画面的执行程序

how to implement executors for splash screen in android

我在 post 上读到,Coroutine 不是在启动画面上使用的好习惯,而 Executors 是最好的,因为 Coroutine 比 Executors 需要更多的时间来启动,但我没有找到一个实现的例子,Executors 是普通的 java Executor class 用来管理线程池吗?

一旦你有了一个Executor实例,你就可以向它提交多个任务,并让它们一个接一个地执行。你不能简单地使用原始线程来做到这一点。

创建一个执行器,该执行器使用一个工作线程在无界队列外运行。 (但是请注意,如果此单个线程在关闭之前的执行过程中因故障而终止,则如果需要执行后续任务,一个新线程将取代它。)保证任务按顺序执行,并且不会有多个任务处于活动状态在任何给定时间。

Executors.newSingleThreadExecutor().execute {
            // todo.
        }

首先,为了获得最佳性能,您必须阅读 William Reed here and here and also this other link here

评论中提到的 link

当你阅读文章时,请关注这些词

The measurements were made on a debuggable app differ surprisingly from production performance

As pointed out by Jake Wharton, the difference is partly due to ExecutorService being preloaded by the Zygote, a special part of the Android framework that shares code between processes. Other concurrency frameworks which, like coroutines, aren’t preloaded, will also have a comparatively high initialization cost. That said, coroutines have a lot of advantages over ExecutorService. They’ve got scopes, suspending functions, they’re much more lightweight than threads, etc. The general recommendation to use them in Android applications is sound, but their impressive feature set has an initialization cost at the moment. Perhaps the Kotlin and Android teams will be able to optimize this in the future. Until then, best to avoid using coroutines in your Application class, or in your main Activity, if startup time is a primary concern.

其次,你问题的另一部分我认为是你问的Executor

这里是如何使用它

1 - 在主线程中执行代码

// Create an executor that executes tasks in the main thread.
val mainExecutor = ContextCompat.getMainExecutor(this)

// Execute a task in the main thread
mainExecutor.execute {
    // You code logic goes here.
}

2 - 在后台线程中执行代码

// Create an executor that executes tasks in a background thread.
val backgroundExecutor: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor()

// Execute a task in the background thread.
backgroundExecutor.execute {
    // Your code logic goes here.
}

// Execute a task in the background thread after 3 seconds.
backgroundExecutor.schedule({
    // Your code logic goes here
}, 3, TimeUnit.SECONDS)

使用后记得关闭执行器

backgroundExecutor.shutdown(); // or backgroundExecutor.shutdownNow();

3 - 在后台线程中执行代码并在主线程中更新 UI。

// Create an executor that executes tasks in the main thread. 
val mainExecutor: Executor = ContextCompat.getMainExecutor(this)

// Create an executor that executes tasks in a background thread.
val backgroundExecutor = Executors.newSingleThreadScheduledExecutor()

// Execute a task in the background thread.
backgroundExecutor.execute {
    // Your code logic goes here.

    // Update UI on the main thread
    mainExecutor.execute {
        // You code logic goes here.
    }
}

什么是最好的性能调试(生产可能如前所述有所不同)?- Executor,Coroutine 需要花费很多时间,因为使用此功能尝试两者

fun TestPerformance(){
    val startTime = System.nanoTime()
    
    // DEBUG in handler 3002952707
    Handler(Looper.getMainLooper()).postDelayed({
        val endTime = System.nanoTime()
        println("DEBUG in handler ${endTime-startTime}")
    }, 3000)

    // DEBUG in backgroundExecutor 3001161822
    val backgroundExecutor: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
    backgroundExecutor.schedule({
        val endTime = System.nanoTime()
        println("DEBUG in backgroundExecutor ${endTime-startTime}")
    }, 3, TimeUnit.SECONDS)

    // DEBUG in GlobalScope 3046312603
    GlobalScope.launch {
        delay(3000)
        val endTime = System.nanoTime()
        println("DEBUG in GlobalScope ${endTime-startTime}")
    }
}

如你所见,我也添加了另一种方式来与处理程序进行比较,并查看代码中的注释,处理程序和执行程序都比协程快,有时处理程序获胜,有时执行程序获胜,它们都给出了相同的性能。

如果您想了解更多有关如何使用处理程序的示例,请查看此 它很有用,我在回答中使用了它。