fixed possible deadlock

This commit is contained in:
Mariotaku Lee 2017-04-11 12:25:48 +08:00
parent cbdf5989b6
commit e0baf8c875
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
1 changed files with 15 additions and 12 deletions

View File

@ -25,7 +25,8 @@ import org.mariotaku.twidere.model.util.AccountUtils.ACCOUNT_USER_DATA_KEYS
import org.mariotaku.twidere.util.JsonSerializer import org.mariotaku.twidere.util.JsonSerializer
import org.mariotaku.twidere.util.ParseUtils import org.mariotaku.twidere.util.ParseUtils
import java.io.IOException import java.io.IOException
import java.util.concurrent.FutureTask import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException import java.util.concurrent.TimeoutException
@ -162,15 +163,16 @@ private fun parseCredentials(authToken: String, @Credentials.Type authType: Stri
} }
internal object AccountDataQueue { internal object AccountDataQueue {
val handler = Handler(Looper.getMainLooper()) private val executor = Executors.newSingleThreadExecutor()
fun getUserData(manager: AccountManager, account: Account, key: String): String? { fun getUserData(manager: AccountManager, account: Account, key: String): String? {
val future = FutureTask<String?> { manager.getUserData(account, key) } val callable = Callable {
if (Thread.currentThread() === Looper.getMainLooper().thread) { manager.getUserData(account, key)
future.run()
} else handler.post {
future.run()
} }
if (Thread.currentThread() === Looper.getMainLooper().thread) {
return callable.call()
}
val future = executor.submit(callable)
try { try {
return future.get(1, TimeUnit.SECONDS) return future.get(1, TimeUnit.SECONDS)
} catch (e: TimeoutException) { } catch (e: TimeoutException) {
@ -179,12 +181,13 @@ internal object AccountDataQueue {
} }
fun peekAuthToken(manager: AccountManager, account: Account, authTokenType: String): String? { fun peekAuthToken(manager: AccountManager, account: Account, authTokenType: String): String? {
val future = FutureTask<String?> { manager.peekAuthToken(account, authTokenType) } val callable = Callable {
if (Thread.currentThread() === Looper.getMainLooper().thread) { manager.peekAuthToken(account, authTokenType)
future.run()
} else handler.post {
future.run()
} }
if (Thread.currentThread() === Looper.getMainLooper().thread) {
return callable.call()
}
val future = executor.submit(callable)
try { try {
return future.get(1, TimeUnit.SECONDS) return future.get(1, TimeUnit.SECONDS)
} catch (e: TimeoutException) { } catch (e: TimeoutException) {