Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android线程将自动挂起_Android_Multithreading_Performance_Runnable_Executor - Fatal编程技术网

Android线程将自动挂起

Android线程将自动挂起,android,multithreading,performance,runnable,executor,Android,Multithreading,Performance,Runnable,Executor,我正在尝试使用多线程进行一些计算。最多我要做100次不同的计算。即使每次我试图平衡10个线程的开始,我也会在Log Cat上看到线程挂起的消息。我认为这是在扼杀表演。当我刚刚启动10个线程时,性能很好 //launch thread for each accuracy values executor = Executors.newFixedThreadPool(100/accuracyInterval); int countThreads = 0;

我正在尝试使用多线程进行一些计算。最多我要做100次不同的计算。即使每次我试图平衡10个线程的开始,我也会在Log Cat上看到线程挂起的消息。我认为这是在扼杀表演。当我刚刚启动10个线程时,性能很好

//launch thread for each accuracy values
        executor = Executors.newFixedThreadPool(100/accuracyInterval);

        int countThreads = 0;

        for(Integer acc: listAccuracies){

            Runnable worker = new MyRunnable(acc);

            if(accuracyInterval != 10) {

                if (countThreads == 10) {

                    while (threadsDoneCounter < 10) {

                        //Log.d("THREADS ","WAITING");

                    }

                    countThreads = 0;
                }

            }
            executor.execute(worker);

            countThreads ++;
        }


        executor.shutdown();

        // Wait until all threads are finish
        while (!executor.isTerminated()) {

        }
当我必须启动例如100个线程时,如何使它具有更好的性能?我做错了什么

谢谢大家的关注,
关于

为什么要启动这么多线程?!我需要100种不同的计算方法。例如,我每次可以启动10个线程。单线程计算时间太长。100个线程会使计算速度变慢,因为它们只会为了CPU的使用而互相争斗,并浪费大量内存——除非您的CPU有100个内核。(正如你所发现的)“如果两个人比一个人好,那么一百个人一定比十个人好”是错误的,我知道。但是我不能通过十个线程,当这十个线程完成时,再启动另一个10吗?这就是我想做的。不。启动十个线程,并对这些线程进行编码,以执行您需要它们执行的任何操作,完成后停止。
public class MyRunnable implements Runnable {
        private final int accuracy;

        MyRunnable(int accuracy) {
            this.accuracy = accuracy;
        }

        @Override
        public void run() {

            Log.d("THREAD " , Integer.toString(accuracy) + " START");

            try {

                calculatePrediction(accuracy);


            } catch (Exception e) {


            }


            threadsDoneCounter++;

            Log.d("THREAD " , Integer.toString(accuracy) + " END");

        }
    }