我怎样才能确保我的线程/进程在不同内核上运行 [英] How can I make sure my threads / processes are running on different cores

查看:126
本文介绍了我怎样才能确保我的线程/进程在不同内核上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想衡量我的code的性能改进,当我运行与单核Android设备多核设备(如S3)在我的多线程的Andr​​oid应用程序。衡量业绩,我按顺序运行的任务与并行。我实现了普通的Java线程,这似乎并没有发挥作用。因此,我试过AsynchTask,但我只得到了性能改进一点点。

你能不能让我知道我可以写code,使确保每个我的任务/线程正在在不同内核上运行,而不是单一的一个?如果这是不可能的,我怎么能最大限度地利用多个核心的我的应用程序?

这里的code为执行任务的活动的onCreate方法。

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_local_executor);    multiplicationTask T1 =新multiplicationTask();
    t1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);    multiplicationTask T2 =新multiplicationTask();
    t2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);    multiplicationTask T3 =新multiplicationTask();
    t3.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);    multiplicationTask T4 =新multiplicationTask();
    t4.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);}

下面是从onCreate方法运行AsynchTask

 类multiplicationTask扩展的AsyncTask<整数,整数,字符串> {    在preExecute保护无效()
    {
        Log.v(的println,执行任务);
    }    @覆盖
    保护字符串doInBackground(整数... PARAMS){
     //做大量的浮点运算独立于任何事情任何
    }
    保护无效onPostExecute(字符串结果){
        Log.v(的println,完成任务:+ resulting_time);
    }}


解决方案

  

可以让我知道我怎么能写code,使确保每个我的任务/线程正在在不同内核上运行,而不是一个单一?


这将由JVM / Android的自动处理。如果您没有看到任何的性能提升,最有可能的原因是:


  • 的任务不parallelisable(比如你计算两个结果,但第二个取决于第一个让他们按顺序运行)

  • 的任务没有CPU绑定的(即如果你读一个巨大的文件,瓶颈就是你的存储,而不是CPU的速度,并增加线程不会帮助)

  • 您不启动足够的线程/启动线程过多

我建议你展示code创建和启动线程以及给什么样的任务做,如果你需要一个更具体的答案的想法。

修改

注意的AsyncTask的主要用途是运行与UI交互短后台任务。在你的情况,一个简单的执行者可能会更好。基本语法创建一个将是:

 私有静态最终诠释N_CPUS =调用Runtime.getRuntime()availableProcessors()+ 1。
私人最终ExecutorService的执行人= Executors.newFixedThreadPool(N_CPUS);

和使用它:

  executor.submit(新的Runnable(){
    公共无效的run(){
        Log.v(的println,执行任务);
        //你的code在这里
        Log.v(的println,完成任务:+ resulting_time);
    }
});

和完成后不要忘记关闭执行人。

现在的性能的改善将在许多因素而有所不同。在你的情况,如果任务过于短暂的,上下文切换开销(当CPU启动一个线程或其他)可以足够大,使得多线程的好处是可以抵消。

另一个可能的瓶颈是同步的:如果你在线程之间交换数据不断,这将导致大量的开销太大。

I'm trying to measure performance improvement of my code when I run my multithreaded android app on a multicore device (like the S3) versus a single core android device. To measure performance, I run the tasks sequentially versus in parallel. I've implemented normal Java threads, which didn't seem to make a difference. I thus tried AsynchTask, but I only got a little bit of performance improvement.

Can you let me know how I can write code that makes sure that each of my tasks / threads are being run on different cores as opposed to a single one? If that is not possible, how can I maximize the use of multiple cores for my app?

Here's the code for the onCreate method of the activity that executes the tasks.

protected void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_local_executor);

    multiplicationTask t1 = new multiplicationTask();
    t1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);       

    multiplicationTask t2 = new multiplicationTask();
    t2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    multiplicationTask t3 = new multiplicationTask();
    t3.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    multiplicationTask t4 = new multiplicationTask();
    t4.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

}

Here is the AsynchTask that is run from the onCreate method

class multiplicationTask extends AsyncTask<Integer, Integer, String> {

    protected void onPreExecute()
    {
        Log.v("PrintLN", "Executing Task");
    }

    @Override
    protected String doInBackground(Integer... params) {
     //Do lots of floating point operations that are independent of anything whatsoever
    }


    protected void onPostExecute(String result) {
        Log.v("PrintLN", "Done Task: " + resulting_time);
    }

}

解决方案

Can you let me know how I can write code that makes sure that each of my tasks / threads are being run on different cores as opposed to a single one?

That will be handled automatically by the JVM / android. If you don't see any performance gains, the likeliest reasons are:

  • the tasks are not parallelisable (for example you calculate two results but the second depends on the first so they run sequentially)
  • the tasks are no CPU-bound (i.e. if you read a huge file, the bottleneck is the speed of you storage, not the CPU, and adding threads won't help)
  • you don't start enough threads / you start too many threads

I suggest you show the code that create and start the threads as well as give an idea of what the tasks do if you need a more specific answer.

EDIT

Note that AsyncTask's primary use is to run short background tasks that interact with the UI. In your case, a plain executor would probably be better. The basic syntax to create one would be:

private static final int N_CPUS = Runtime.getRuntime().availableProcessors() + 1;
private final ExecutorService executor = Executors.newFixedThreadPool(N_CPUS);

And to use it:

executor.submit(new Runnable() {
    public void run() {
        Log.v("PrintLN", "Executing Task");
        //your code here
        Log.v("PrintLN", "Done Task: " + resulting_time);
    }
});

And don't forget to shutdown the executor when you are done.

Now the performance improvement will vary on a number of factors. In your case, if tasks are too short lived, the context switching overhead (when the CPU activates one thread or another) can be large enough that the benefits of multiple threads can be offset.

Another possible bottleneck is synchronization: if you exchange data across threads continuously this will cause a lot of overhead too.

这篇关于我怎样才能确保我的线程/进程在不同内核上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆