简体   繁体   English

使用多线程设置线程优先级无效

[英]Setting a thread priority with multithreading has no effect

The problem is when I start executing heavy task using more threads than one while playing music - the playback is cracking (Setting numOfCores = 2 or more). 问题是当我在播放音乐时使用多于一个的线程开始执行繁重的任务时 - 播放开始崩溃(设置numOfCores = 2或更多)。 If I set numOfCores = 1 the playback is ok. 如果我设置numOfCores = 1 ,播放就可以了。

How can I avoid this? 我怎么能避免这个?

NOTE: 注意:

  • I don't want to use throttling because I want have done my tasks as fast as I can. 我不想使用限制因为我想尽可能快地完成我的任务。
  • If I play music using another application is fine. 如果我使用其他应用程序播放音乐很好。

I. Playing music using openSL ES via JNI in a Foreground Service. I.在前台服务中通过JNI使用openSL ES播放音乐。

II. II。 This code of AsyncTask that is started in another service. 这个AsyncTask代码是在另一个服务中启动的。

@Override
    protected Void doInBackground(String... params)
    {
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
        class Worker implements Runnable
        {
            @Override
            public void run()
            {
              android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
                //Heavy work using jni here
            }
        }
        numOfCores = 1; //The phone has 8 cores
        final ExecutorService es = Executors.newFixedThreadPool(numOfCores);
        //Adding the workers to es
        for(...)
        {
            es.execute(new Worker());
        }
        es.shutdown();
        es.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);



     }

@jameslarge Could you explain please? @jameslarge你能解释一下吗?

Your code example is pretty sketchy, and I don't know android, but I looked up that setThreadPriority() function, and the documentation clearly says that it sets the priority of the calling thread. 你的代码示例非常粗略,我知道android,但我查找了setThreadPriority()函数,文档清楚地说它设置了调用线程的优先级。

The same thread that sets its own priority in your example then goes on to create a thread pool with either one or two worker threads. 在您的示例中设置自己的优先级的相同线程继续创建具有一个或两个工作线程的线程池。 Those workers are different threads from the one that set is own priority. 这些工作者与设置优先级的工作者不同。

Your complaint is that, when there are two workers, the sound playback "crackles", but when there's only one worker the sound playback is OK. 你的抱怨是,当有两个工作人员时,声音回放“噼里啪啦”,但当只有一个工作人员时,声音播放就可以了。 That makes sense: If the Android device has two processors, and your thread pool has only one worker, then there will always be one processor that is available to do other stuff (eg, play sound), but if you create the thread pool with two workers, then the workers can tie up both CPUs, and leave no available CPU to play the sound. 这是有道理的:如果Android设备有两个处理器,并且您的线程池只有一个工作线程,那么总会有一个处理器可用于执行其他工作(例如,播放声音),但是如果您创建线程池两个工人,然后工人可以占用两个CPU,并留下没有可用的CPU来播放声音。

So, the thread-pool workers are the problem. 因此,线程池工作者就是问题所在。 But your code does not lower the priority of the thread pool workers. 但是您的代码不会降低线程池工作者的优先级。 It lowers the priority of the thread that created the thread pool. 它降低了创建线程池的线程的优先级。 Maybe, for all I know, that's even the same thread that is trying to play the sound. 也许,就我所知,这甚至是尝试播放声音的同一个主题。

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

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