简体   繁体   English

如何在Android中并行运行线程

[英]How to run threads in parallel in Android

I'm trying to run multiple threads in parallel. 我正在尝试并行运行多个线程。 I tried to achieve this by having multiple instances of a thread. 我试图通过拥有一个线程的多个实例来实现这一点。 My assumption is that it will be executed simultaneously; 我的假设是它将同时执行; however, the threads are being executed in a sequence. 但是,线程正在按顺序执行。

Here's a simple code I tested: 这是我测试的一个简单代码:

new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i=0; i<100; i++) {
                LMLog.info("THREAD", "Counting " + i + " in Thread A");
            }
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i=0; i<100; i++) {
                LMLog.info("THREAD", "Counting " + i + " in Thread B");
            }
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i=0; i<100; i++) {
                LMLog.info("THREAD", "Counting " + i + " in Thread C");
            }
        }
    }).start();

And the log shows that the for loops are being executed one after another 并且日志显示for循环正在逐个执行

Counting from 0 to 99 in Thread A , followed by the same thing in Thread B and Thread C . 在线程A从0到99计数,然后在线程B和线程C

Based on this, I concluded that they were not being executed in parallel as I thought they would be, but rather in a sequence. 基于此,我得出结论,他们并没有像我想象的那样被并行执行,而是按顺序执行。

How can I achieve parallel execution in Android then? 那我怎么能在Android中实现并行执行呢?

I might be wrong, but I think your conclusion is faulty. 我可能错了,但我认为你的结论是错误的。 The reason for it is that counting to 100 takes such a short time that it probably takes more time to instantiate a new Thread and start it. 原因是计数到100需要这么短的时间,以至于可能需要更多时间来实例化一个新线程并启动它。 If you want to ensure that your test is "valid" you should first instantiate all 3 threads and then start them all, something like this (also increase counter to 10k or 100k): 如果你想确保你的测试是“有效的”,你应该首先实例化所有3个线程,然后全部启动它们,这样的事情(也增加到10k或100k的计数器):

Thread t1 = new Thread(new Runnable() {
    @Override
    public void run() {
        for (int i=0; i<10000; i++) {
            LMLog.info("THREAD", "Counting " + i + " in Thread A");
        }
    }
Thread t2 = new Thread(new Runnable() {
    @Override
    public void run() {
        for (int i=0; i<10000; i++) {
            LMLog.info("THREAD", "Counting " + i + " in Thread B");
        }
    }
Thread t3 = new Thread(new Runnable() {
    @Override
    public void run() {
        for (int i=0; i<10000; i++) {
            LMLog.info("THREAD", "Counting " + i + " in Thread C");
        }
    }
t1.start(); t2.start(); t3.start();

This is purely for making your 'test' work. 这纯粹是为了让你的'测试'工作。 You can also use AsyncTask or some other methods. 您还可以使用AsyncTask或其他一些方法。

最好使用AsyncTask ,您可以使用Thread_POOL_EXECUTOR并行运行多个任务。

Use the Excutors class they provide a variety of thread pools. 使用Excutors类,它们提供各种线程池。

ExecutorService pool = Executors.newFixedThreadPool(numOfYourThreads)

But beware not to start too many threads and stress your CPU too much. 但要注意不要启动太多线程并过分强调CPU。

Edit: As I see the canon seems to be AsyncTask which only applies to short operations. 编辑:因为我看到佳能似乎是AsyncTask,它只适用于短操作。 If you have to use a longer Task either use a IntentService or a ExecutorService. 如果必须使用更长的Task,请使用IntentService或ExecutorService。

This is how to use ExecutorService 这是如何使用ExecutorService

ExecutorService executorService = Executors.newFixedThreadPool(10);
  executorService.execute(new Runnable() {
        public void run() {
            // do something
        }
    });

executorService.shutdown();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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