Procházet zdrojové kódy

修复取消多线程安全问题

drake před 4 roky
rodič
revize
1ddb9fd35c

+ 5 - 4
kalle/src/main/java/com/yanzhenjie/kalle/NetCancel.kt

@@ -15,10 +15,11 @@
  */
 package com.yanzhenjie.kalle
 
+import java.util.concurrent.ConcurrentHashMap
 
 object NetCancel {
 
-    private val map = mutableMapOf<Canceller, Any>()
+    private val cancellerMap = ConcurrentHashMap<Canceller, Any>()
 
     /**
      * 添加一个网络请求取消者用于取消网络
@@ -29,7 +30,7 @@ object NetCancel {
     @Synchronized
     fun add(uid: Any?, canceller: Canceller) {
         uid ?: return
-        map[canceller] = uid
+        cancellerMap[canceller] = uid
     }
 
     /**
@@ -40,7 +41,7 @@ object NetCancel {
     @Synchronized
     fun remove(uid: Any?) {
         uid ?: return
-        val iterator = map.iterator()
+        val iterator = cancellerMap.iterator()
         while (iterator.hasNext()) {
             if (iterator.next().value == uid) iterator.remove()
         }
@@ -54,7 +55,7 @@ object NetCancel {
     @Synchronized
     fun cancel(uid: Any?) {
         uid ?: return
-        val iterator = map.iterator()
+        val iterator = cancellerMap.iterator()
         while (iterator.hasNext()) {
             val next = iterator.next()
             if (uid == next.value) {

+ 2 - 6
net/src/main/java/com/drake/net/NetConfig.kt

@@ -82,8 +82,7 @@ object NetConfig {
             else -> app.getString(R.string.net_other_error)
         }
 
-        if (logEnabled)
-            printStackTrace()
+        if (logEnabled) printStackTrace()
         app.toast(message)
     }
 
@@ -93,10 +92,7 @@ object NetConfig {
             is RequestParamsException,
             is ResponseException,
             is NullPointerException -> onError(this)
-            else -> {
-                if (logEnabled)
-                    printStackTrace()
-            }
+            else -> if (logEnabled) printStackTrace()
         }
     }
 }

+ 1 - 2
net/src/main/java/com/drake/net/scope/AndroidScope.kt

@@ -95,8 +95,7 @@ open class AndroidScope(
      * 错误处理
      */
     open fun handleError(e: Throwable) {
-        if (NetConfig.logEnabled)
-            e.printStackTrace()
+        if (NetConfig.logEnabled) e.printStackTrace()
     }
 
     open fun cancel(cause: CancellationException? = null) {

+ 2 - 4
net/src/main/java/com/drake/net/utils/Fastest.kt

@@ -56,8 +56,7 @@ suspend fun <T> CoroutineScope.fastest(
                 val allFail = listDeferred.all { it.isCancelled }
                 if (allFail) deferred.completeExceptionally(e) else {
                     if (e !is CancellationException) {
-                        if (NetConfig.logEnabled)
-                            e.printStackTrace()
+                        if (NetConfig.logEnabled) e.printStackTrace()
                     }
                 }
             }
@@ -104,8 +103,7 @@ suspend fun <T, R> CoroutineScope.fastest(
                 val allFail = listDeferred.all { it.deferred.isCancelled }
                 if (allFail) deferred.completeExceptionally(e) else {
                     if (e !is CancellationException) {
-                        if (NetConfig.logEnabled)
-                            e.printStackTrace()
+                        if (NetConfig.logEnabled) e.printStackTrace()
                     }
                 }
             }

+ 9 - 10
sample/src/main/java/com/drake/net/sample/ui/fragment/FastestFragment.kt

@@ -61,22 +61,21 @@ class FastestFragment : Fragment() {
         假设并发的接口返回的数据类型不同  或者 想要监听最快请求返回的结果回调请使用 [Deferred.transform] 函数
         具体请看文档 https://liangjingkanji.github.io/Net/fastest/
         */
-
         // scopeNetLife {
         //
         //     // 同时发起四个网络请求
-        //     val deferred = Get<String>("api").transform {
-        //         23
-        //     } // 错误接口
-        //     val deferred1 = Get<String>("api1").transform {
-        //         50
-        //     } // 错误接口
-        //     val deferred2 = Post<String>("api").transform {
-        //         100
+        //     val requestList = mutableListOf<DeferredTransform<String, String>>().apply {
+        //         for (i in 0..28) {
+        //             val request = Get<String>("api").transform {
+        //                 Log.d("日志", "(FastestFragment.kt:73)    it = ${it}")
+        //                 it
+        //             }
+        //             add(request)
+        //         }
         //     }
         //
         //     // 只返回最快的请求结果
-        //     tv_fragment.text = fastest(deferred, deferred1, deferred2).toString()
+        //     tv_fragment.text = fastest(requestList).toString()
         // }
     }
 }