diff --git a/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt b/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt index 8506d92..a1d9c03 100644 --- a/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt +++ b/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt @@ -3,6 +3,7 @@ package com.amaze.filepreloaderlibrary import android.app.Activity import com.amaze.filepreloaderlibrary.datastructures.DataContainer import com.amaze.filepreloaderlibrary.datastructures.FetcherFunction +import com.amaze.filepreloaderlibrary.utils.LIB_CONTEXT import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import java.lang.ref.WeakReference @@ -49,7 +50,7 @@ object FilePreloader { * Get all the loaded data, this will load the data in the current thread if it's not loaded. */ fun getAllDataLoaded(activity: Activity, getList: (List?) -> Unit) { - GlobalScope.launch { + GlobalScope.launch(LIB_CONTEXT) { val preloaded = PreloadedManager.getAllLoaded() activity.runOnUiThread { @@ -63,7 +64,7 @@ object FilePreloader { * Clear everything, all metadata loaded will be discarded. */ fun cleanUp() { - GlobalScope.launch { + GlobalScope.launch(LIB_CONTEXT) { weakList.forEach { it.get()?.clear() } diff --git a/lib/src/main/java/com/amaze/filepreloaderlibrary/Loader.kt b/lib/src/main/java/com/amaze/filepreloaderlibrary/Loader.kt index eb3d86f..7166149 100644 --- a/lib/src/main/java/com/amaze/filepreloaderlibrary/Loader.kt +++ b/lib/src/main/java/com/amaze/filepreloaderlibrary/Loader.kt @@ -102,7 +102,7 @@ internal class Loader(private val clazz: Class) { * on each file (represented by its path) inside the folder. */ internal fun loadFolder(unit: ProcessUnit) { - GlobalScope.launch { + GlobalScope.launch(LIB_CONTEXT) { val file = KFile(unit.path) val fileList = file.list() ?: arrayOf() diff --git a/lib/src/main/java/com/amaze/filepreloaderlibrary/Processor.kt b/lib/src/main/java/com/amaze/filepreloaderlibrary/Processor.kt index 9f4ead2..93bdb34 100644 --- a/lib/src/main/java/com/amaze/filepreloaderlibrary/Processor.kt +++ b/lib/src/main/java/com/amaze/filepreloaderlibrary/Processor.kt @@ -56,7 +56,7 @@ internal class Processor(private val clazz: Class) { } private fun work(producer: ReceiveChannel?>, onStart: suspend () -> Unit, onEnd: suspend () -> Unit = {}) { - val job = GlobalScope.launch { + val job = GlobalScope.launch(LIB_CONTEXT) { onStart() for (elem in producer) { @@ -81,7 +81,7 @@ internal class Processor(private val clazz: Class) { } internal fun clear() { - GlobalScope.launch { + GlobalScope.launch(LIB_CONTEXT) { ranCoroutines.forEach { it.cancelAndJoin() } diff --git a/lib/src/main/java/com/amaze/filepreloaderlibrary/SpecializedPreloader.kt b/lib/src/main/java/com/amaze/filepreloaderlibrary/SpecializedPreloader.kt index f419188..4c42990 100644 --- a/lib/src/main/java/com/amaze/filepreloaderlibrary/SpecializedPreloader.kt +++ b/lib/src/main/java/com/amaze/filepreloaderlibrary/SpecializedPreloader.kt @@ -6,6 +6,7 @@ import com.amaze.filepreloaderlibrary.datastructures.DataContainer import com.amaze.filepreloaderlibrary.datastructures.FetcherFunction import com.amaze.filepreloaderlibrary.utils.DIVIDER import com.amaze.filepreloaderlibrary.utils.KFile +import com.amaze.filepreloaderlibrary.utils.LIB_CONTEXT import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -35,7 +36,7 @@ class SpecializedPreloader(private val clazz: Class, * Get the loaded data. [getList] will run on UI thread. */ fun load(activity: Activity, path: String, getList: (List) -> Unit) { - GlobalScope.launch { + GlobalScope.launch(LIB_CONTEXT) { val t: Pair>? = loader.getLoaded(path) if (t != null) { diff --git a/lib/src/main/java/com/amaze/filepreloaderlibrary/utils/Constants.kt b/lib/src/main/java/com/amaze/filepreloaderlibrary/utils/Constants.kt index 3a2d54b..1162ec7 100644 --- a/lib/src/main/java/com/amaze/filepreloaderlibrary/utils/Constants.kt +++ b/lib/src/main/java/com/amaze/filepreloaderlibrary/utils/Constants.kt @@ -1,6 +1,10 @@ package com.amaze.filepreloaderlibrary.utils +import android.os.AsyncTask import com.amaze.filepreloaderlibrary.datastructures.PreloadedFolder +import kotlinx.coroutines.ObsoleteCoroutinesApi +import kotlinx.coroutines.newFixedThreadPoolContext +import kotlin.math.ceil /** * Contains a hash map linking paths to [PreloadedFolder]s. @@ -19,4 +23,13 @@ const val PRIORITY_NOW = 0 /** * Probably will need to be loaded in the FUTURE */ -const val PRIORITY_FUTURE = 1 \ No newline at end of file +const val PRIORITY_FUTURE = 1 + +private val CORES_AVAILABLE = Runtime.getRuntime().availableProcessors() + +/** + * Normally coroutines use up all the CPUs available, wasting resources, like energy. + * This limits the amount of threads to at most half +1 of the cores of the processor. + */ +@ObsoleteCoroutinesApi +val LIB_CONTEXT = newFixedThreadPoolContext(ceil(CORES_AVAILABLE/2.toFloat()).toInt(), "FilePreloaderLibrary") \ No newline at end of file