下面列出了android.os.Process#myTid ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void open(int fd) {
LogUtils.logi(IOHook.class.getSimpleName() + " openAfterCall fd:" + fd);
final IOContext ioContext = getIOContext();
ioContext.state = IOContext.OPEN;
ioContext.ioRecord.fd = fd;
ioContext.ioRecord.path = Utils.getPathByFD(fd);
ioContext.ioRecord.openTime = System.currentTimeMillis();
ioContext.ioRecord.process = Utils.getProcessName();
ioContext.ioRecord.thread = Thread.currentThread().getName();
ioContext.ioRecord.processId = Process.myPid();
ioContext.ioRecord.threadId = Process.myTid();
ioContext.ioRecord.stacktrace = getStackTrace();
mPools.put(getKey(ioContext.ioRecord.fd), ioContext);
}
@Override
public final void run() {
if (isQuit) {
return;
}
Thread.currentThread().setName(theTaskName);
mTid = Process.myTid();
Looper.prepare();
if (isQuit) {
return;
}
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(taskPriority);
taskDispatchHandler = new WeakHandler<>(this, mLooper);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
private void unbindItemInfosAndClearQueuedBindRunnables() {
if (sWorkerThread.getThreadId() == Process.myTid()) {
throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
"main thread");
}
// Clear any deferred bind runnables
synchronized (mDeferredBindRunnables) {
mDeferredBindRunnables.clear();
}
// Remove any queued UI runnables
mHandler.cancelAll();
// Unbind all the workspace items
unbindWorkspaceItemsOnMainThread();
}
/** Runs the specified runnable immediately if called from the main thread, otherwise it is
* posted on the main thread handler. */
private void runOnMainThread(Runnable r) {
if (sWorkerThread.getThreadId() == Process.myTid()) {
// If we are on the worker thread, post onto the main handler
mHandler.post(r);
} else {
r.run();
}
}
/** Runs the specified runnable immediately if called from the worker thread, otherwise it is
* posted on the worker thread handler. */
private static void runOnWorkerThread(Runnable r) {
if (sWorkerThread.getThreadId() == Process.myTid()) {
r.run();
} else {
// If we are not on the worker thread, then post to the worker handler
sWorker.post(r);
}
}
private void onlyBindAllApps() {
final Callbacks oldCallbacks = mCallbacks.get();
if (oldCallbacks == null) {
// This launcher has exited and nobody bothered to tell us. Just bail.
Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
return;
}
// shallow copy
@SuppressWarnings("unchecked")
final ArrayList<AppInfo> list
= (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Runnable r = new Runnable() {
public void run() {
final long t = SystemClock.uptimeMillis();
final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
if (callbacks != null) {
callbacks.bindAllApplications(list);
}
if (DEBUG_LOADERS) {
Log.d(TAG, "bound all " + list.size() + " apps from cache in "
+ (SystemClock.uptimeMillis()-t) + "ms");
}
}
};
boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
if (isRunningOnMainThread) {
r.run();
} else {
mHandler.post(r);
}
}
@Override
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
/** Runs the specified runnable immediately if called from the main thread, otherwise it is
* posted on the main thread handler. */
@Thunk void runOnMainThread(Runnable r) {
if (sWorkerThread.getThreadId() == Process.myTid()) {
// If we are on the worker thread, post onto the main handler
mHandler.post(r);
} else {
r.run();
}
}
/** Runs the specified runnable immediately if called from the worker thread, otherwise it is
* posted on the worker thread handler. */
@Thunk static void runOnWorkerThread(Runnable r) {
if (sWorkerThread.getThreadId() == Process.myTid()) {
r.run();
} else {
// If we are not on the worker thread, then post to the worker handler
sWorker.post(r);
}
}
private void onlyBindAllApps() {
final Callbacks oldCallbacks = mCallbacks.get();
if (oldCallbacks == null) {
// This launcher has exited and nobody bothered to tell us. Just bail.
Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
return;
}
// shallow copy
@SuppressWarnings("unchecked")
final ArrayList<AppInfo> list
= (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
final WidgetsModel widgetList = mBgWidgetsModel.clone();
Runnable r = new Runnable() {
public void run() {
final long t = SystemClock.uptimeMillis();
final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
if (callbacks != null) {
callbacks.bindAllApplications(list);
callbacks.bindAllPackages(widgetList);
}
if (DEBUG_LOADERS) {
Log.d(TAG, "bound all " + list.size() + " apps from cache in "
+ (SystemClock.uptimeMillis()-t) + "ms");
}
}
};
boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
if (isRunningOnMainThread) {
r.run();
} else {
mHandler.post(r);
}
}
private void runOnMainThread(Runnable r, int type) {
if (sWorkerThread.getThreadId() == Process.myTid()) {
// If we are on the worker thread, post onto the main handler
mHandler.post(r);
} else {
r.run();
}
}
/**
* Runs the specified runnable immediately if called from the worker thread,
* otherwise it is posted on the worker thread handler.
*/
private static void runOnWorkerThread(Runnable r) {
if (sWorkerThread.getThreadId() == Process.myTid()) {
r.run();
} else {
// If we are not on the worker thread, then post to the worker
// handler
sWorker.post(r);
}
}
static Pair<Long, int[]> findNextAvailableIconSpace(Context context,
String name, Intent launchIntent, int firstScreenIndex,
ArrayList<Long> workspaceScreens) {
// Lock on the app so that we don't try and get the items while apps are
// being added
LauncherAppState app = LauncherAppState.getInstance();
LauncherModel model = app.getModel();
boolean found = false;
synchronized (app) {
if (sWorkerThread.getThreadId() != Process.myTid()) {
// Flush the LauncherModel worker thread, so that if we just did
// another
// processInstallShortcut, we give it time for its shortcut to
// get added to the
// database (getItemsInLocalCoordinates reads the database)
model.flushWorkerThread();
}
final ArrayList<ItemInfo> items = LauncherModel
.getItemsInLocalCoordinates(context);
// Try adding to the workspace screens incrementally, starting at
// the default or center
// screen and alternating between +1, -1, +2, -2, etc. (using ~
// ceil(i/2f)*(-1)^(i-1))
firstScreenIndex = Math.min(firstScreenIndex,
workspaceScreens.size());
int count = workspaceScreens.size();
for (int screen = firstScreenIndex; screen < count && !found; screen++) {
int[] tmpCoordinates = new int[2];
if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
workspaceScreens.get(screen))) {
// Update the Launcher db
return new Pair<Long, int[]>(workspaceScreens.get(screen),
tmpCoordinates);
}
}
}
return null;
}
public void unbindItemInfosAndClearQueuedBindRunnables() {
if (sWorkerThread.getThreadId() == Process.myTid()) {
throw new RuntimeException(
"Expected unbindLauncherItemInfos() to be called from the "
+ "main thread");
}
// Clear any deferred bind runnables
mDeferredBindRunnables.clear();
// Remove any queued bind runnables
mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
// Unbind all the workspace items
unbindWorkspaceItemsOnMainThread();
}
static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Intent launchIntent,
int firstScreenIndex,
ArrayList<Long> workspaceScreens) {
// Lock on the app so that we don't try and get the items while apps are being added
LauncherAppState app = LauncherAppState.getInstance();
LauncherModel model = app.getModel();
boolean found = false;
synchronized (app) {
if (sWorkerThread.getThreadId() != Process.myTid()) {
// Flush the LauncherModel worker thread, so that if we just did another
// processInstallShortcut, we give it time for its shortcut to get added to the
// database (getItemsInLocalCoordinates reads the database)
model.flushWorkerThread();
}
final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
// Try adding to the workspace screens incrementally, starting at the default or center
// screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
int count = workspaceScreens.size();
for (int screen = firstScreenIndex; screen < count && !found; screen++) {
int[] tmpCoordinates = new int[2];
if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
workspaceScreens.get(screen))) {
// Update the Launcher db
return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
}
}
}
return null;
}
public void unbindItemInfosAndClearQueuedBindRunnables() {
if (sWorkerThread.getThreadId() == Process.myTid()) {
throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
"main thread");
}
// Clear any deferred bind runnables
synchronized (mDeferredBindRunnables) {
mDeferredBindRunnables.clear();
}
// Remove any queued bind runnables
mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
// Unbind all the workspace items
unbindWorkspaceItemsOnMainThread();
}
public static ThreadInfo getCurrent() {
return new ThreadInfo(Thread.currentThread(), Process.myTid());
}
public static void assertWorkerThread() {
if (LauncherAppState.isDogfoodBuild() &&
(LauncherModel.sWorkerThread.getThreadId() != Process.myTid())) {
throw new IllegalStateException();
}
}
Event(String name) {
mName = name;
mThreadId = Process.myTid();
mBeginTimeMs = SystemClock.elapsedRealtime();
}
/**
* Returns a key which consists of |name| and the ID of the current thread.
* The key is used with pending events making them thread-specific, thus avoiding
* an exception when similarly named events are started from multiple threads.
*/
@VisibleForTesting
static String makeEventKeyForCurrentThread(String name) {
return name + "@" + Process.myTid();
}