Skip to content

Commit daf316c

Browse files
Merge pull request #142 from velocitycareerlabs/VL-8447-v2.6.7-refactor-thread-executor
v2.6.7 refactor thread executor
2 parents c02a53b + 191dd48 commit daf316c

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

VCL/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
defaultConfig {
1313
minSdk 24
1414
targetSdk 34
15-
versionName "2.6.6"
16-
versionCode 150
15+
versionName "2.6.7"
16+
versionCode 151
1717
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1818
consumerProguardFiles "consumer-rules.pro"
1919
}

VCL/src/main/java/io/velocitycareerlabs/impl/data/infrastructure/executors/ExecutorImpl.kt

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ package io.velocitycareerlabs.impl.data.infrastructure.executors
1010
import android.os.Handler
1111
import android.os.Looper
1212
import io.velocitycareerlabs.impl.domain.infrastructure.executors.Executor
13+
import io.velocitycareerlabs.impl.utils.VCLLog
1314
import java.util.concurrent.ExecutorService
1415
import java.util.concurrent.Executors
1516
import java.util.concurrent.TimeUnit
1617

17-
internal class ExecutorImpl: Executor {
18+
internal class ExecutorImpl : Executor {
19+
20+
private val TAG = ExecutorImpl::class.simpleName
1821

1922
private val mainThread: Handler = Handler(Looper.getMainLooper())
20-
private val backgroundThreadPool: ExecutorService = Executors.newFixedThreadPool(10)
23+
24+
private var numberOfCores = Runtime.getRuntime().availableProcessors()
25+
private var optimalThreadCount = minOf(numberOfCores * 2, 8) // Limit max to 8 threads
26+
27+
private val executorService: ExecutorService =
28+
Executors.newFixedThreadPool(optimalThreadCount, Executors.defaultThreadFactory())
2129

2230
override fun runOnMain(block: () -> Unit) {
2331
mainThread.post {
@@ -26,13 +34,24 @@ internal class ExecutorImpl: Executor {
2634
}
2735

2836
override fun runOnBackground(block: () -> Unit) {
29-
backgroundThreadPool.submit {
30-
block()
37+
executorService.submit {
38+
try {
39+
block()
40+
} catch (e: Exception) {
41+
VCLLog.e(TAG, "", e)
42+
}
3143
}
3244
}
3345

3446
override fun shutdown() {
35-
backgroundThreadPool.shutdown()
36-
backgroundThreadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS)
47+
executorService.shutdown()
48+
try {
49+
if (!executorService.awaitTermination(60 * 3, TimeUnit.SECONDS)) {
50+
executorService.shutdownNow() // Force shutdown if not completed within 3 minutes
51+
}
52+
} catch (e: InterruptedException) {
53+
executorService.shutdownNow() // Force shutdown on interruption
54+
Thread.currentThread().interrupt()
55+
}
3756
}
38-
}
57+
}

0 commit comments

Comments
 (0)