2017-05-24 6 views
0

私は、(プロセス名、アイコン、メモリなどの)アプリケーションプロパティを実行して、listviewに表示するアプリケーションを作成しようとしています。スレッドを使用してアプリケーションの読み込み時間を減らす方法はありますか?

私はメインスレッドで実行しているので、これは時間がかかりすぎています。 このサンプルループでスレッドをさらに作成するにはどうすればよいですか? (私はアンドロイドのプログラミングに新しいです)

//would like to run this loop in parallel 
for (int i = 0; i < processes.size(); i++) { 
// calculations 
} 

答えて

0

複数AsyncTasksを使用して試してみて、task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)を使用してタスクを実行したり、並列に処理するために複数のスレッドを使用しています。 スレッド

Thread thread1 = new Thread(new Runnable() { 
    @Override 
    public void run() { 
     for (int i = 0; i < processes.size() /2; i++) { 
     // calculations 
     } 
    } 
}); 

Thread thread2 = new Thread(new Runnable() { 
    @Override 
    public void run() { 
     for (int i = processes.size() /2; i < processes.size(); i++) { 
     // calculations 
     } 
    } 
}); 

thread1.start(); 
thread1.start(); 
+0

私はあなたの両方の方法を試してみましたが、それはAsyncTaskやクラッシュにリストが表示されません..お返事ありがとうございましたを使用して

AsyncTask

new AsyncTask<Void, Void, Void>() { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { //would like to run this loop in parallel //You can also start threads for (int i = 0; i < processes.size(); i++) { // calculations } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 

スレッド内。 –

0
Hi I think you have one loop to iterate which is the data got from any Web Service. 

- Basically all the long running process which are need for UI changes can be run inside the AsyncTask, which will create background threads. 

class AsyncExaple extends AsyncTask<Void, Void, Void>{ 

    @Override 
    protected Void doInBackground(Void... params) { 

     //What ever the long running tasks are run inside this block 
     //would like to run this loop in parallel 
     for (int i = 0; i < processes.size(); i++) { 
// calculations 
     } 
     return null; 
    } 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 
    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
    } 
}; 


To call this AsyncTask do as follows 

AsyncExaple asyncExaple = new AsyncExaple(); 
asyncExaple.execute(); 


If you still want to use the Threads use below code: 

new Thread(new Runnable() { 
      @Override 
      public void run() { 

      } 
     }).start(); 
関連する問題