Merge branch 'dev_arch_opt_3.0' into 'dev_robobus-m1-p-app-module_1.1.0_230112_1.1.0'
[dev_arch_opt_3.0] 代码逻辑优化 See merge request zhjt/AndroidApp/MoGoEagleEye!682
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.mogo.eagle.core.utilcode.util;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 根据对象的类名,返回一致的hashCode
|
||||
* 如果缓存中不存在,则创建hashCode并存入缓存
|
||||
* 如果缓存中存在,直接用缓存数据
|
||||
*/
|
||||
public class ObjectHashCodeUtils {
|
||||
|
||||
private static final ConcurrentHashMap<String, Integer> hashCodes = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
public static int getHashCodeIfNeed(Object obj) {
|
||||
if (obj == null) {
|
||||
return -1;
|
||||
}
|
||||
String name = obj.getClass().getName();
|
||||
Integer value = hashCodes.get(name);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
int hashCode = System.identityHashCode(obj);
|
||||
hashCodes.put(name, hashCode);
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ public final class ThreadUtils {
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
r.run();
|
||||
} else {
|
||||
int what = r.hashCode();
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
HANDLER.removeMessages(what);
|
||||
Message msg = Message.obtain(HANDLER, r);
|
||||
msg.what = what;
|
||||
@@ -75,7 +75,7 @@ public final class ThreadUtils {
|
||||
}
|
||||
|
||||
public static void runOnUiThreadDelayed(final Runnable r, long delayMillis) {
|
||||
int what = r.hashCode();
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
HANDLER.removeMessages(what);
|
||||
Message msg = Message.obtain(HANDLER, r);
|
||||
msg.what = what;
|
||||
|
||||
@@ -11,7 +11,7 @@ public class UiThreadHandler {
|
||||
private UiThreadHandler() { }
|
||||
|
||||
public static boolean post( Runnable r ) {
|
||||
int what = r.hashCode();
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
sUiHandler.removeMessages(what);
|
||||
Message msg = Message.obtain(sUiHandler, r);
|
||||
msg.what = what;
|
||||
@@ -19,7 +19,7 @@ public class UiThreadHandler {
|
||||
}
|
||||
|
||||
public static boolean postDelayed( Runnable r, long delayMillis ) {
|
||||
int what = r.hashCode();
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
sUiHandler.removeMessages(what);
|
||||
Message msg = Message.obtain(sUiHandler, r);
|
||||
msg.what = what;
|
||||
@@ -27,7 +27,7 @@ public class UiThreadHandler {
|
||||
}
|
||||
|
||||
public static void removeCallbacks( Runnable r ) {
|
||||
int what = r.hashCode();
|
||||
int what = ObjectHashCodeUtils.getHashCodeIfNeed(r);
|
||||
sUiHandler.removeMessages(what);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user