Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: Use shared ThreadPoolExecutor avoid p_thread create OOM.
  • Loading branch information
PonyCui committed Jul 24, 2019
commit 16dbb05b7325c9323ea13e5fc59282fc649806e8
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SVGADynamicEntity {

fun setDynamicImage(url: String, forKey: String) {
val handler = android.os.Handler()
thread {
SVGAParser.threadPoolExecutor.execute {
(URL(url).openConnection() as? HttpURLConnection)?.let {
try {
it.connectTimeout = 20 * 1000
Expand Down
16 changes: 9 additions & 7 deletions library/src/main/java/com/opensource/svgaplayer/SVGAParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SVGAParser(private val context: Context) {
val cancelBlock = {
cancelled = true
}
Thread {
threadPoolExecutor.execute {
try {
if (HttpResponseCache.getInstalled() == null && !noCache) {
Log.e("SVGAParser", "SVGAParser can not handle cache before install HttpResponseCache. see https://github.com/yyued/SVGAPlayer-Android#cache")
Expand All @@ -63,7 +63,7 @@ class SVGAParser(private val context: Context) {
outputStream.write(buffer, 0, count)
}
if (cancelled) {
return@Thread
return@execute
}
ByteArrayInputStream(outputStream.toByteArray()).use {
complete(it)
Expand All @@ -75,18 +75,20 @@ class SVGAParser(private val context: Context) {
e.printStackTrace()
failure(e)
}
}.start()
}
return cancelBlock
}

}

var fileDownloader = FileDownloader()
private var threadPoolBlockingQueue = LinkedBlockingQueue<Runnable>()
private var threadPoolExecutor = ThreadPoolExecutor(3, 10, 60000, TimeUnit.MILLISECONDS, this.threadPoolBlockingQueue)

protected fun finalize() {
threadPoolExecutor.shutdown()
companion object {
private val threadPoolBlockingQueue = LinkedBlockingQueue<Runnable>()
internal var threadPoolExecutor = ThreadPoolExecutor(3, 10, 60000, TimeUnit.MILLISECONDS, threadPoolBlockingQueue)
fun setThreadPoolExecutor(executor: ThreadPoolExecutor) {
threadPoolExecutor = executor
}
}

fun decodeFromAssets(name: String, callback: ParseCompletion) {
Expand Down