18034727885
2026-06-26 253f680fb9666751e0f9a8e5e8f2f9e3eb1894e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.example.basecore.util;
 
/**
 */
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
 
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
 
/**
 * Helper methods to deal with threading related tasks.
 */
public class ThreadUtils {
 
    /**
     * Run the supplied Runnable on the main thread. The method will block until
     * the Runnable completes.
     *
     * @param r The Runnable to run.
     */
    public static void runOnUiThreadBlocking(final Runnable r) {
        if (runningOnUiThread()) {
            r.run();
        } else {
            FutureTask<Void> task = new FutureTask<Void>(r, null);
            postOnUiThread(task);
            try {
                task.get();
            } catch (Exception e) {
                throw new RuntimeException("Exception occured while waiting for runnable", e);
            }
        }
    }
 
    /**
     * Run the supplied Callable on the main thread, wrapping any exceptions in
     * a RuntimeException. The method will block until the Callable completes.
     *
     * @param c The Callable to run
     * @return The result of the callable
     */
    public static <T> T runOnUiThreadBlockingNoException(Callable<T> c) {
        try {
            return runOnUiThreadBlocking(c);
        } catch (ExecutionException e) {
            throw new RuntimeException("Error occured waiting for callable", e);
        }
    }
 
    /**
     * Run the supplied Callable on the main thread, The method will block until
     * the Callable completes.
     *
     * @param c The Callable to run
     * @return The result of the callable
     * @throws ExecutionException c's exception
     */
    public static <T> T runOnUiThreadBlocking(Callable<T> c) throws ExecutionException {
        FutureTask<T> task = new FutureTask<T>(c);
        runOnUiThread(task);
        try {
            return task.get();
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted waiting for callable", e);
        }
    }
 
    /**
     * Run the supplied FutureTask on the main thread. The method will block
     * only if the current thread is the main thread.
     *
     * @param task The FutureTask to run
     * @return The queried task (to aid inline construction)
     */
    public static <T> FutureTask<T> runOnUiThread(FutureTask<T> task) {
        if (runningOnUiThread()) {
            task.run();
        } else {
            postOnUiThread(task);
        }
        return task;
    }
 
    /**
     * Run the supplied Callable on the main thread. The method will block
     * only if the current thread is the main thread.
     *
     * @param c The Callable to run
     * @return A FutureTask wrapping the callable to retrieve results
     */
    public static <T> FutureTask<T> runOnUiThread(Callable<T> c) {
        return runOnUiThread(new FutureTask<T>(c));
    }
 
    /**
     * Run the supplied Runnable on the main thread. The method will block
     * only if the current thread is the main thread.
     *
     * @param r The Runnable to run
     */
    public static void runOnUiThread(Runnable r) {
        if (runningOnUiThread()) {
            r.run();
        } else {
            LazyHolder.sUiThreadHandler.post(r);
        }
    }
 
    /**
     * Run the supplied Runnable on the main thread. The method will block
     * only if the current thread is the main thread.
     *
     * @param r The Runnable to run
     */
    public static void runOnUiThreadDelayed(Runnable r,long millSeconds) {
        LazyHolder.sUiThreadHandler.postDelayed(r, millSeconds);
    }
 
    /**
     * Post the supplied FutureTask to run on the main thread. The method will
     * not block, even if called on the UI thread.
     *
     * @param task The FutureTask to run
     * @return The queried task (to aid inline construction)
     */
    public static <T> FutureTask<T> postOnUiThread(FutureTask<T> task) {
        LazyHolder.sUiThreadHandler.post(task);
        return task;
    }
 
    /**
     * Post the supplied Runnable to run on the main thread. The method will
     * not block, even if called on the UI thread.
     *
     * @param task The Runnable to run
     */
    public static void postOnUiThread(Runnable r) {
        LazyHolder.sUiThreadHandler.post(r);
    }
 
    public static void postOnUiThreadDelay(Runnable r, long delay) {
        LazyHolder.sUiThreadHandler.postDelayed(r, delay);
    }
 
    /**
     * 如果当前在主线程则立即执行r,否则post到主线程执行
     */
    public static void dispatchOnUiThread(Runnable r) {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            r.run();
        }
        else {
            postOnUiThread(r);
        }
    }
 
 
    /**
     * Asserts that the current thread is running on the main thread.
     */
    public static void assertOnUiThread() {
        assert runningOnUiThread();
    }
 
    /**
     * @return true iff the current thread is the main (UI) thread.
     */
    public static boolean runningOnUiThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }
 
    /**
     * Set thread priority to audio.
     */
//    @CalledByNative
    public static void setThreadPriorityAudio(int tid) {
        Process.setThreadPriority(tid, Process.THREAD_PRIORITY_AUDIO);
    }
 
    public static Handler getUiThreadHandler() {
        return LazyHolder.sUiThreadHandler;
    }
 
    private static class LazyHolder {
        private static Handler sUiThreadHandler = new Handler(Looper.getMainLooper());
    }
}