Java Code Examples for android.os.Process#getThreadPriority()

The following examples show how to use android.os.Process#getThreadPriority() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreadUtils.java    From litho with Apache License 2.0 6 votes vote down vote up
/**
 * Try to raise the priority of {@param threadId} to {@param targetThreadPriority}.
 *
 * @return the original thread priority of the target thread.
 */
public static int tryRaiseThreadPriority(int threadId, int targetThreadPriority) {
  // Main thread is about to be blocked, raise the running thread priority.
  final int originalThreadPriority = Process.getThreadPriority(threadId);
  boolean success = false;
  while (!success && targetThreadPriority < originalThreadPriority) {
    // Keep trying to increase thread priority of running thread as long as it is an increase.
    try {
      Process.setThreadPriority(threadId, targetThreadPriority);
      success = true;
    } catch (SecurityException e) {
      /*
       From {@link Process#THREAD_PRIORITY_DISPLAY}, some applications can not change
       the thread priority to that of the main thread. This catches that potential error
       and tries to set a lower priority.
      */
      targetThreadPriority += Process.THREAD_PRIORITY_LESS_FAVORABLE;
    }
  }
  return originalThreadPriority;
}
 
Example 2
Source File: ThreadUtils.java    From litho with Apache License 2.0 6 votes vote down vote up
/**
 * Try to raise the priority of {@param threadId} to {@param targetThreadPriority}.
 *
 * @return the original thread priority of the target thread.
 */
public static int tryRaiseThreadPriority(int threadId, int targetThreadPriority) {
  // Main thread is about to be blocked, raise the running thread priority.
  final int originalThreadPriority = Process.getThreadPriority(threadId);
  boolean success = false;
  while (!success && targetThreadPriority < originalThreadPriority) {
    // Keep trying to increase thread priority of running thread as long as it is an increase.
    try {
      Process.setThreadPriority(threadId, targetThreadPriority);
      success = true;
    } catch (SecurityException e) {
      /*
       From {@link Process#THREAD_PRIORITY_DISPLAY}, some applications can not change
       the thread priority to that of the main thread. This catches that potential error
       and tries to set a lower priority.
      */
      targetThreadPriority += Process.THREAD_PRIORITY_LESS_FAVORABLE;
    }
  }
  return originalThreadPriority;
}
 
Example 3
Source File: VideoPlayer.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
private void init(InputHolder inputHolder) throws IOException {
	synchronized (inputLock) {
		if (pointer == 0 && !consumed) {
			int priority = Process.getThreadPriority(Process.myTid());
			Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY);
			try {
				this.inputHolder = inputHolder;
				pointer = init(new NativeBridge(this), seekAnyFrame);
				int errorCode = getErrorCode(pointer);
				if (errorCode != 0) {
					free();
					throw new InitializationException(errorCode);
				}
				initialized = true;
				seekerThread.start();
			} finally {
				Process.setThreadPriority(priority);
			}
		}
	}
}
 
Example 4
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void onGetSuggestionsMultiple(
        TextInfo[] textInfos, int suggestionsLimit, boolean sequentialWords) {
    int pri = Process.getThreadPriority(Process.myTid());
    try {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        mListener.onGetSuggestions(
                mSession.onGetSuggestionsMultiple(
                        textInfos, suggestionsLimit, sequentialWords));
    } catch (RemoteException e) {
    } finally {
        Process.setThreadPriority(pri);
    }
}
 
Example 5
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void onCancel() {
    int pri = Process.getThreadPriority(Process.myTid());
    try {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        mSession.onCancel();
    } finally {
        Process.setThreadPriority(pri);
    }
}
 
Example 6
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void onClose() {
    int pri = Process.getThreadPriority(Process.myTid());
    try {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        mSession.onClose();
    } finally {
        Process.setThreadPriority(pri);
        mListener = null;
    }
}
 
Example 7
Source File: ImageLoaderManager.java    From appcan-android with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void run() {
    /* 设置任务线程为后台线程,获得更少的执行机会,减少对UI渲染的影响 */
    int threadPriority = Process.getThreadPriority(Process.myTid());
    if (threadPriority != Process.THREAD_PRIORITY_LOWEST) {
        Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
    }
    final ImageLoadTask loadTask = seekReadyTask();
    if (loadTask == null) {
        handler.sendEmptyMessage(ACTION_LOAD_FINISH);
        return;
    }
    try {
        // 从磁盘去取
        Bitmap bitmap = DiskCache.readCache(loadTask.getKey());
        if (bitmap == null) {// 尚未缓存,从源去取
            bitmap = loadFromSource(loadTask);
        }
        if (bitmap != null) {// 取得图片成功
            // 将bitmap放入LruCache缓存列表
            memoryCache.put(loadTask.filePath, bitmap);
        }
        final Bitmap finalBitmap = bitmap;
        if (loadTask.getCallBack() != null) {
            handler.post(new Runnable() {

                @Override
                public void run() {
                    loadTask.performCallback(finalBitmap);
                }
            });
        }
    } catch (OutOfMemoryError e) {
        memoryCache.evictAll();
        System.gc();
        e.printStackTrace();
        BDebug.e(TAG, "OutOfMemoryError!!!!!!!!!!!!!!!!!!!!:" + e.getMessage());
    } finally {
        if (loadTask != null) {
            synchronized (taskList) {
                taskList.remove(loadTask);
            }
        }
    }
}
 
Example 8
Source File: ViewRenderer.java    From android-viewer-for-khan-academy with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Constructs a new {@link ViewRenderer} object that coordinates concurrent
 * background rendering for display to views. It is primarily designed for
 * use with {@link AdapterView} based views. Supplying zero for the thread
 * count will cause the {@link ViewRenderer} to use a single thread that
 * can be shared among other instances.
 * 
 * @param maxThreadCount
 *            the number of threads that will be created to support
 *            background rendering, may be zero.
 * @param passes
 *            the number of rendering passes required to complete the
 *            rendering required for a view
 * @param mayInterruptIfRunning
 *            whether background rendering tasks can be interrupted if they
 *            become redundant
 * @param cacheCapacity
 *            the maximum number of render
 * 
 */

public ViewRenderer(int threadCount, int passes, boolean mayInterruptIfRunning, int cacheCapacity) {
	if (threadCount < 0) throw new IllegalArgumentException("negative thread count");
	if (passes <= 0) throw new IllegalArgumentException("passes not positive");
	if (cacheCapacity < 0) throw new IllegalArgumentException("negative cache capacity");
	mExecutor = threadCount == 0 ? getDefaultExecutor() : new Executor(threadCount);
	mPasses = passes;
	mMayInterruptIfRunning = mayInterruptIfRunning;
	mCache = cacheCapacity == 0 ? null : new Cache(cacheCapacity);
	mHandler = new Handler();
	mInheritedThreadPriority = Process.getThreadPriority(Process.myTid());
}
 
Example 9
Source File: ThreadUtils.java    From cronet with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Checks whether Thread priority is THREAD_PRIORITY_AUDIO or not.
 * @param tid Thread id.
 * @return true for THREAD_PRIORITY_AUDIO and false otherwise.
 */
@CalledByNative
private static boolean isThreadPriorityAudio(int tid) {
    return Process.getThreadPriority(tid) == Process.THREAD_PRIORITY_AUDIO;
}
 
Example 10
Source File: ThreadUtils.java    From 365browser with Apache License 2.0 2 votes vote down vote up
/**
 * Checks whether Thread priority is THREAD_PRIORITY_AUDIO or not.
 * @param tid Thread id.
 * @return true for THREAD_PRIORITY_AUDIO and false otherwise.
 */
@CalledByNative
private static boolean isThreadPriorityAudio(int tid) {
    return Process.getThreadPriority(tid) == Process.THREAD_PRIORITY_AUDIO;
}