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

您的位置:首頁技術(shù)文章
文章詳情頁

Android如何調(diào)整線程調(diào)用棧大小

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

在常規(guī)的Android開發(fā)過程中,隨著業(yè)務(wù)邏輯越來越復(fù)雜,調(diào)用棧可能會(huì)越來越深,難免會(huì)遇到調(diào)用棧越界的情況,這種情況下,就需要調(diào)整線程棧的大小。

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

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

主線程的線程棧是沒有辦法進(jìn)行修改的,這個(gè)沒辦法處理。

針對(duì)線程池的情況,需要在創(chuàng)建線程的時(shí)候,調(diào)用構(gòu)造函數(shù)

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

通過設(shè)置stackSize參數(shù)來解決問題。

參考代碼如下:

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的情況,一般是通過調(diào)用

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

指定線程池來運(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)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 91精品久久| 一区二区三区中文国产亚洲 | 91精品啪在线看国产网站 | 亚洲国产精品久久久久秋霞不卡 | 日韩三级视频在线观看 | 久久香蕉国产线看观看精品yw | 久久久久久色 | 美女视频黄视大全视频免费网址 | 日本一级aaaa特黄毛片 | 免费一级毛片在线播放不收费 | 中国国产一级毛片视频 | 日韩亚洲在线 | 久久国产免费一区二区三区 | 亚洲一区中文 | 欧美色爱综合 | 中国a级黄色片 | 欧洲免费无线码二区5 | 国产精品激情丝袜美女 | 午夜黄色福利视频 | 欧美孕交视频 | 亚洲欧美精品成人久久91 | 国产亚洲小视频 | 美国一级片在线观看 | 免费观看a视频 | 久久久黄色大片 | 中文字幕成人免费高清在线视频 | 色播亚洲精品网站 亚洲第一 | 亚洲欧美网 | 男人的天堂在线观看视频不卡 | 国内精品福利视频 | 国产精品视_精品国产免费 国产精品视频久 | 欧美成人免费公开播放 | 午夜宅男在线永久免费观看网 | 国产亚洲精品久久久久久无 | 美国一级毛片片aa免 | 神马午夜-午夜片 | 欧美高清一区二区三 | 欧美国产高清欧美 | 欧美在线1 | 国产精品videosse | 欧美一级欧美一级在线播放 |