最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年10月20日 15:02:26   作者:默默的點滴  
這篇文章主要介紹了Android如何調整線程調用棧大小,幫助大家更好的進行Android開發(fā),完善自身程序,感興趣的朋友可以了解下

在常規(guī)的Android開發(fā)過程中,隨著業(yè)務邏輯越來越復雜,調用??赡軙絹碓缴睿y免會遇到調用棧越界的情況,這種情況下,就需要調整線程棧的大小。

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

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

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

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

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();
      }
    }
  }
}

參考鏈接

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

相關文章

  • Android引入OpenCV的示例

    Android引入OpenCV的示例

    本篇文章主要介紹了Android引入OpenCV的示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Flutter框架實現Android拖動到垃圾桶刪除效果

    Flutter框架實現Android拖動到垃圾桶刪除效果

    這篇文章主要介紹了Flutter框架實現Android拖動到垃圾桶刪除效果,Flutter框架中的Draggable部件,用于支持用戶通過手勢拖動,它是基于手勢的一種方式,可以使用戶可以在屏幕上拖動指定的部件,下面我們來詳細了解一下
    2023-12-12
  • Android音頻系統(tǒng)AudioTrack使用方法詳解

    Android音頻系統(tǒng)AudioTrack使用方法詳解

    這篇文章主要為大家詳細介紹了Android音頻系統(tǒng)AudioTrack的使用方法,如何使用AudioTrack進行音頻播放,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android下載gradle失敗的解決方法

    Android下載gradle失敗的解決方法

    這篇文章主要介紹了Android下載gradle失敗的解決方法,文章通過圖文結合的方式給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-06-06
  • 詳解Glide4.0集成及使用注意事項

    詳解Glide4.0集成及使用注意事項

    這篇文章主要介紹了詳解Glide4.0集成及使用注意事項,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Android開發(fā)之Picasso通過URL獲取用戶頭像的圓形顯示

    Android開發(fā)之Picasso通過URL獲取用戶頭像的圓形顯示

    這篇文章主要介紹了android開發(fā)之Picasso通過URL獲取用戶頭像的圓形顯示,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-06-06
  • Android編程之文件讀寫操作與技巧總結【經典收藏】

    Android編程之文件讀寫操作與技巧總結【經典收藏】

    這篇文章主要介紹了Android編程之文件讀寫操作與技巧,結合實例形式總結分析了Android常見的文件與目錄的讀寫操作,及相關函數的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • Kotlin基本類型自動裝箱一點問題剖析

    Kotlin基本類型自動裝箱一點問題剖析

    這篇文章主要剖析了Kotlin基本類型自動裝箱的一點問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • android利用handler實現打地鼠游戲

    android利用handler實現打地鼠游戲

    這篇文章主要為大家詳細介紹了android利用handler實現打地鼠游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Android提高之Service用法實例解析

    Android提高之Service用法實例解析

    這篇文章主要介紹了Android的Service用法,很實用的功能,需要的朋友可以參考下
    2014-08-08

最新評論

兴海县| 云林县| 贺州市| 大同县| 太仆寺旗| 旺苍县| 左云县| 三台县| 新田县| 东海县| 蓝田县| 赤城县| 文山县| 商水县| 张北县| 海口市| 昌黎县| 桐梓县| 任丘市| 霞浦县| 博罗县| 皮山县| 青浦区| 沂水县| 泌阳县| 格尔木市| 义乌市| 延津县| 兖州市| 博乐市| 皮山县| 永仁县| 鸡泽县| 乌拉特后旗| 襄樊市| 广昌县| 延津县| 齐河县| 普宁市| 通城县| 临泉县|