Android如何調(diào)整線程調(diào)用棧大小
在常規(guī)的Android開(kāi)發(fā)過(guò)程中,隨著業(yè)務(wù)邏輯越來(lái)越復(fù)雜,調(diào)用??赡軙?huì)越來(lái)越深,難免會(huì)遇到調(diào)用棧越界的情況,這種情況下,就需要調(diào)整線程棧的大小。
當(dāng)然,主要還是增大線程棧大小,尤其是存在jni調(diào)用的情況下,C++層的棧開(kāi)銷(xiāo)有時(shí)候是非??植赖?,比如說(shuō)遞歸調(diào)用。
這就需要分三種情況,主線程,自定義線程池,AsyncTask。
主線程的線程棧是沒(méi)有辦法進(jìn)行修改的,這個(gè)沒(méi)辦法處理。
針對(duì)線程池的情況,需要在創(chuàng)建線程的時(shí)候,調(diào)用構(gòu)造函數(shù)
public Thread(@RecentlyNullable ThreadGroup group, @RecentlyNullable Runnable target, @RecentlyNonNull String name, long stackSize)
通過(guò)設(shè)置stackSize
參數(shù)來(lái)解決問(wèn)題。
參考代碼如下:
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"); // 工作線程堆棧大小調(diào)整為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(); } } } }
針對(duì)AsyncTask的情況,一般是通過(guò)調(diào)用
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec, Params... params)
指定線程池來(lái)運(yùn)行,在特定的線程池中調(diào)整線程棧的大小。
參考代碼如下:
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:增加調(diào)用堆棧大小
- AsyncTask和線程池
以上就是Android如何調(diào)整線程調(diào)用棧大小的詳細(xì)內(nèi)容,更多關(guān)于Android 調(diào)整調(diào)用棧大小的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android開(kāi)發(fā)中TextView各種常見(jiàn)使用方法小結(jié)
這篇文章主要介紹了Android開(kāi)發(fā)中TextView各種常見(jiàn)使用方法,結(jié)合實(shí)例形式總結(jié)分析了Android開(kāi)發(fā)中TextView各種常見(jiàn)布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04android studio3.3.1代碼提示忽略大小寫(xiě)的設(shè)置
這篇文章主要介紹了android studio3.3.1代碼提示忽略大小寫(xiě)的設(shè)置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android調(diào)用系統(tǒng)默認(rèn)瀏覽器訪問(wèn)的方法
這篇文章主要介紹了Android調(diào)用系統(tǒng)默認(rèn)瀏覽器訪問(wèn)的方法的相關(guān)資料,需要的朋友可以參考下2016-03-03android利用ContentResolver訪問(wèn)者獲取手機(jī)聯(lián)系人信息
這篇文章主要介紹了android利用ContentResolver訪問(wèn)者獲取手機(jī)聯(lián)系人信息,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02Android程序報(bào)錯(cuò)程序包org.apache.http不存在問(wèn)題的解決方法
這篇文章主要介紹了Android程序報(bào)錯(cuò)"程序包org.apache.http不存在——Android 6.0已經(jīng)不支持HttpClient" 問(wèn)題的解決方法,感興趣的小伙伴們可以參考一下2016-06-06Android編程實(shí)現(xiàn)獲取新浪天氣預(yù)報(bào)數(shù)據(jù)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲取新浪天氣預(yù)報(bào)數(shù)據(jù)的方法,涉及Android基于新浪接口的調(diào)用及數(shù)據(jù)處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Flutter移動(dòng)端進(jìn)行多渠道打包發(fā)布的全過(guò)程
在使用flutter開(kāi)發(fā)的過(guò)程中,需要根據(jù)不同的環(huán)境,不同的包名來(lái)打包,下面這篇文章主要給大家介紹了關(guān)于Flutter移動(dòng)端進(jìn)行多渠道打包發(fā)布的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06Flutter Android應(yīng)用啟動(dòng)白屏的解決方案
任何一個(gè)app基本都會(huì)設(shè)計(jì)一個(gè)啟動(dòng)頁(yè),今天我們就來(lái)看看怎么在flutter項(xiàng)目中設(shè)置啟動(dòng)頁(yè),這篇文章主要給大家介紹了關(guān)于Flutter Android應(yīng)用啟動(dòng)白屏解決的相關(guān)資料,需要的朋友可以參考下2021-11-11