3

In my application I have to load data from about 20 text files in the beginning, only once. I was using Asynctasks on AsyncTask.THREAD_POOL_EXECUTOR first, but the KEEP_ALIVE_TIME for each asynctask was an issue, destroying asynctasks which were queued for long. Is there a way to change this default KEEP_ALIVE_TIME? Please tell me if so. Right now I have switched to the following code: -


public class DBLoader {

    public static int completed = 0;
    public static int nthreads;
    Executor executor;

    public DBLoader(int nthreads) {
        executor = java.util.concurrent.Executors.newFixedThreadPool(nthreads);
        this.nthreads = nthreads;
    }

    public void startThread(Thread thread){
        executor.execute(thread);
    }
}

Each of my threads reads a file from raw resource and puts it into local db.


InputStream is = InputStreamReader(context.getResources().openRawResource(R.raw.filename));

Right now it takes about 50 seconds for this on MOTO G1, when I am reading 8 files. And it it not acceptable. Please suggest a better way to do this. I am using greendao for implementing the local db. Thanks in advance.

  • 1
    X-Y-Problem. Do not use text files to init your db. – Fildor Dec 09 '15 at 08:39
  • 1
    keepalive does not kill tasks: http://stackoverflow.com/questions/10379314/how-does-keep-alive-work-with-threadpoolexecutor it just removes threads in the pool that are no longer needed. Your problem lies elsewhere. Is https://github.com/jgilfelt/android-sqlite-asset-helper maybe what you need? – zapl Dec 09 '15 at 09:00
  • Got it! I'll use a .db file to init my db. Thanks fildor! And that link clarified my concepts of keep_alive. Thanks zapl! Found a way to read db from assets here http://stackoverflow.com/questions/9630288/how-to-create-database-from-assets-in-android-using-greendao-orm-library . And again sorry to sound so noob but how can i get this .db file from text file? Can i somehow use the .db that my application makes everytime? where do i find it? – Chinmay Joshi Dec 09 '15 at 09:48

1 Answers1

0

The static variable THREAD_POOL_EXECUTOR is constructed like so:

    public static final Executor THREAD_POOL_EXECUTOR
        = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
                TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);

I don't see why you can't just construct your own ThreadPoolExecutor with the KEEP_ALIVE time modified, copying any private params along the way.

asadmshah
  • 1,338
  • 9
  • 7