下面列出了android.os.Looper#getThread ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Invokes the provided {@link Callable} on the main thread and blocks until the operation
* completes or times out. If this method is invoked on the main thread, the {@code Callable} is
* invoked immediately and no timeout is enforced.
*
* <p>Exceptions thrown by the {@code Callable} are rethrown by this method.
*
* @return result returned by the {@code Callable}.
*/
private static <V> V runOnMainSyncWithTimeoutAndWithCheckedExceptionsExpected(
Callable<V> callable) throws Exception {
Looper mainLooper = Looper.getMainLooper();
if (mainLooper.getThread() == Thread.currentThread()) {
// This method is being invoked on the main thread -- invoke the Callable inline to avoid
// a deadlock.
return callable.call();
} else {
FutureTask<V> task = new FutureTask<V>(callable);
new Handler(mainLooper).post(task);
try {
return task.get(UI_ACTION_EFFECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof Exception) {
throw (Exception) cause;
} else {
throw new RuntimeException("Execution on main thread failed", e);
}
}
}
}
private static PriorityQueue<ByteBuffer> getReclaimed() {
Looper mainLooper = Looper.getMainLooper();
if (mainLooper != null) {
if (Thread.currentThread() == mainLooper.getThread())
return null;
}
return reclaimed;
}
private static PriorityQueue<ByteBuffer> getReclaimed() {
Looper mainLooper = Looper.getMainLooper();
if (mainLooper != null) {
if (Thread.currentThread() == mainLooper.getThread())
return null;
}
return reclaimed;
}
/**
* コンストラクタ
* @param looper
*/
private HandlerThreadHandler(@NonNull final Looper looper, final boolean async) {
super(looper);
final Thread thread = looper.getThread();
mId = thread != null ? thread.getId() : 0;
mAsynchronous = async;
}
/**
* コンストラクタ
* @param looper
* @param callback
*/
private HandlerThreadHandler(@NonNull final Looper looper,
@Nullable final Callback callback, final boolean async) {
super(looper, callback);
final Thread thread = looper.getThread();
mId = thread != null ? thread.getId() : 0;
mAsynchronous = async;
}