国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android如何調整線程調用棧大小

瀏覽:108日期:2022-09-22 09:31:47

在常規的Android開發過程中,隨著業務邏輯越來越復雜,調用棧可能會越來越深,難免會遇到調用棧越界的情況,這種情況下,就需要調整線程棧的大小。

當然,主要還是增大線程棧大小,尤其是存在jni調用的情況下,C++層的棧開銷有時候是非常恐怖的,比如說遞歸調用。

這就需要分三種情況,主線程,自定義線程池,AsyncTask。

主線程的線程棧是沒有辦法進行修改的,這個沒辦法處理。

針對線程池的情況,需要在創建線程的時候,調用構造函數

public Thread(@RecentlyNullable ThreadGroup group, @RecentlyNullable Runnable target, @RecentlyNonNull String name, long stackSize)

通過設置stackSize參數來解決問題。

參考代碼如下:

import android.support.annotation.NonNull;import android.util.Log;import java.util.concurrent.ThreadFactory;/** * A ThreadFactory implementation which create new threads for the thread pool. */public class SimpleThreadFactory implements ThreadFactory { private static final String TAG = 'SimpleThreadFactory'; private final static ThreadGroup group = new ThreadGroup('SimpleThreadFactoryGroup'); // 工作線程堆棧大小調整為2MB private final static int workerStackSize = 2 * 1024 * 1024; @Override public Thread newThread(@NonNull final Runnable runnable) { final Thread thread = new Thread(group, runnable, 'PoolWorkerThread', workerStackSize); // A exception handler is created to log the exception from threads thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(@NonNull Thread thread, @NonNull Throwable ex) {Log.e(TAG, thread.getName() + ' encountered an error: ' + ex.getMessage()); } }); return thread; }}

import android.support.annotation.AnyThread;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.util.Log;import java.util.concurrent.BlockingQueue;import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Future;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;/** * A Singleton thread pool */public class ThreadPool { private static final String TAG = 'ThreadPool'; private static final int KEEP_ALIVE_TIME = 1; private static volatile ThreadPool sInstance = null; private static int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors(); private final ExecutorService mExecutor; private final BlockingQueue<Runnable> mTaskQueue; // Made constructor private to avoid the class being initiated from outside private ThreadPool() { // initialize a queue for the thread pool. New tasks will be added to this queue mTaskQueue = new LinkedBlockingQueue<>(); Log.d(TAG, 'Available cores: ' + NUMBER_OF_CORES); mExecutor = new ThreadPoolExecutor(NUMBER_OF_CORES, NUMBER_OF_CORES * 2, KEEP_ALIVE_TIME, TimeUnit.SECONDS, mTaskQueue, new SimpleThreadFactory()); } @NonNull @AnyThread public static ThreadPool getInstance() { if (null == sInstance) { synchronized (ThreadPool.class) {if (null == sInstance) { sInstance = new ThreadPool();} } } return sInstance; } private boolean isThreadPoolAlive() { return (null != mExecutor) && !mExecutor.isTerminated() && !mExecutor.isShutdown(); } @Nullable @AnyThread public <T> Future<T> submitCallable(@NonNull final Callable<T> c) { synchronized (this) { if (isThreadPoolAlive()) {return mExecutor.submit(c); } } return null; } @Nullable @AnyThread public Future<?> submitRunnable(@NonNull final Runnable r) { synchronized (this) { if (isThreadPoolAlive()) {return mExecutor.submit(r); } } return null; } /* Remove all tasks in the queue and stop all running threads */ @AnyThread public void shutdownNow() { synchronized (this) { mTaskQueue.clear(); if ((!mExecutor.isShutdown()) && (!mExecutor.isTerminated())) {mExecutor.shutdownNow(); } } }}

針對AsyncTask的情況,一般是通過調用

public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec, Params... params)

指定線程池來運行,在特定的線程池中調整線程棧的大小。

參考代碼如下:

import android.os.AsyncTask;import android.support.annotation.AnyThread;import android.support.annotation.NonNull;import android.util.Log;import java.util.concurrent.BlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;public abstract class AsyncTaskEx<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> { private static final String TAG = 'AsyncTaskEx'; private static final int KEEP_ALIVE_TIME = 1; private static volatile ThreadPool sInstance = null; private static int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors(); private final ExecutorService mExecutor; private final BlockingQueue<Runnable> mTaskQueue; public AsyncTaskEx() { // initialize a queue for the thread pool. New tasks will be added to this queue mTaskQueue = new LinkedBlockingQueue<>(); Log.d(TAG, 'Available cores: ' + NUMBER_OF_CORES); mExecutor = new ThreadPoolExecutor(NUMBER_OF_CORES, NUMBER_OF_CORES * 2, KEEP_ALIVE_TIME, TimeUnit.SECONDS, mTaskQueue, new SimpleThreadFactory()); } public AsyncTask<Params, Progress, Result> executeAsync(@NonNull final Params... params) { return super.executeOnExecutor(mExecutor, params); } /* Remove all tasks in the queue and stop all running threads */ @AnyThread public void shutdownNow() { synchronized (this) { mTaskQueue.clear(); if ((!mExecutor.isShutdown()) && (!mExecutor.isTerminated())) {mExecutor.shutdownNow(); } } }}

參考鏈接

Increase AsyncTask stack size? StackOverFlowError: Stack size 1036KB in AsyncTask Android:增加調用堆棧大小 AsyncTask和線程池

以上就是Android如何調整線程調用棧大小的詳細內容,更多關于Android 調整調用棧大小的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
主站蜘蛛池模板: 特色黄色片| 亚洲精品第一区二区在线 | 成人黄页 | 日韩免费一区二区三区 | 成年人免费观看的视频 | 国产精品久久国产三级国电话系列 | 亚洲第一视频在线观看 | 欧美成人免费在线 | 日韩一区二区三区四区 | 亚洲第一区精品日韩在线播放 | 国产大学生自拍 | 成人影院一区二区三区 | 成人 欧美 | 丝袜精品 欧美 亚洲 自拍 | 国产精品一区高清在线观看 | 草草影院第一页yycccom | 九九九九精品视频在线播放 | 91精品国产福利尤物免费 | 国产网址在线 | 国产成人精品曰本亚洲77美色 | 欧美日韩精品一区二区三区视频播放 | 亚洲国产精品a一区二区三区 | 日本韩国一级毛片中文字幕 | 日本午夜vr影院新入口 | 亚洲人成网址在线观看 | 日韩无砖专区体验区 | 亚洲a级片在线观看 | 午夜毛片视频高清不卡免费 | 欧美三级在线观看视频 | 亚洲最大情网站在线观看 | 日韩免费高清一级毛片 | 奇米888四色在线精品 | 亚州中文 | 被老外玩爽的中国美女视频 | 久久爱噜噜噜噜久久久网 | 婷婷亚洲久悠悠色在线播放 | 三级毛片免费看 | 深夜福利视频在线观看免费视频 | 亚洲一区二区三区国产精品 | 亚洲狠狠ady亚洲精品大秀 | 国产不卡视频在线观看 |