Saturday, August 23, 2014

Android example to execute threads with ExecutorService

ExecutorService is an Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks. This example implement 5 threads to update ProgressBars in background thread. The threads executed by a ExecutorService, with thread pool of 2. Such that only 2 thread is running at any time.


package com.example.androidcountdownprogressbar;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends ActionBarActivity {

Button buttonStart;
ProgressBar progressBar1, progressBar2,
progressBar3, progressBar4, progressBar5;

CountThread countThread1, countThread2,
countThread3, countThread4, countThread5;

ExecutorService executorService = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

buttonStart = (Button)findViewById(R.id.start);
progressBar1 = (ProgressBar)findViewById(R.id.progressbar1);
progressBar2 = (ProgressBar)findViewById(R.id.progressbar2);
progressBar3 = (ProgressBar)findViewById(R.id.progressbar3);
progressBar4 = (ProgressBar)findViewById(R.id.progressbar4);
progressBar5 = (ProgressBar)findViewById(R.id.progressbar5);

buttonStart.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
countThread1 = new CountThread(progressBar1);
countThread2 = new CountThread(progressBar2);
countThread3 = new CountThread(progressBar3);
countThread4 = new CountThread(progressBar4);
countThread5 = new CountThread(progressBar5);

executorService = Executors.newFixedThreadPool(2);
executorService.execute(countThread1);
executorService.execute(countThread2);
executorService.execute(countThread3);
executorService.execute(countThread4);
executorService.execute(countThread5);
}});

}

public class CountThread extends Thread{

ProgressBar progressBar;
final int MAX_PROGRESS = 10;
int progress;

CountThread(ProgressBar progressBar){
this.progressBar = progressBar;
progress = MAX_PROGRESS;
}

@Override
public void run() {

for(int i=0; i<MAX_PROGRESS; i++){
progress--;
MainActivity.this.runOnUiThread(new Runnable(){

@Override
public void run() {
progressBar.setProgress(progress);
}});

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidcountdownprogressbar.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />

<ProgressBar
android:id="@+id/progressbar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="0" />
<ProgressBar
android:id="@+id/progressbar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="0" />
<ProgressBar
android:id="@+id/progressbar3"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="0" />
<ProgressBar
android:id="@+id/progressbar4"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="0" />
<ProgressBar
android:id="@+id/progressbar5"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="0" />
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start" />

</LinearLayout>

No comments:

Post a Comment