简体   繁体   中英

How to allocate a task to each processor core?

I am performing basic encryption/decryption on a Quad core android phone.

Currently encryption time is 1.45 second per file. For 200 files thats around 5 minutes.
I could improve 4 times in performance if I used all 4 cores.

for(i=0;i<200;i++)
{
     encrypt(file[i]);
}

would be a simplified pseudocode, I am using now.

How can I call the encrypt function parallely on all 4 processor cores in android java programming?

Use ExecutorService to perform tasks in parallel: http://developer.android.com/reference/java/util/concurrent/ExecutorService.html

Something like this:

    ExecutorService executorService = Executors.newFixedThreadPool(CORE_COUNT);
    for (int i = 0; i < 200; i++) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                encrypt(file[i]);
            }
        });
    }

    // Waiting for completion
    while (!executorService.awaitTermination(100L, TimeUnit.MILLISECONDS);

One option is to use ExecutorService ( http://developer.android.com/reference/java/util/concurrent/ExecutorService.html ). In your loop, wrap each call to encrypt in a Runnable or Callable and collect the Future's in a list. Then iterate over the futures to wait until they have all finished and handle the results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM