create multiple threads with Executor Service in android. Create parallel thread in android using Executors. Threads in android
In some situation we need to run threads parallely and not sequencially to save time.
so we use ExecutorService to run a certain number of threads to run parallely at the same time.
useful video:https://youtu.be/9w8Te1sn1d0
github code : https://github.com/deepeshdm/ExecutorServcieAndroid
Step1] Create a Runnable class ,since we need to pass a runnable object to the executor to run on the thread.
Bike.java
package com.deepesh.threadapp;
import android.util.Log;
public class Bike implements Runnable{
private int Threadno;
public Bike(int Threadno){
this.Threadno=Threadno;
}
@Override
public void run() {
Log.d("Keyy","START" + Thread.currentThread().getName()+ Threadno);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("Keyy","END" + Thread.currentThread().getName()+ Threadno);
}
}
we use a pure java class - java.util.concurrent
and these classes :=
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Step2] In the Main activity or the java code, define a Varibale of type ExecutorService
and define the number of threads you want to run parallely at the same point.
public class MainActivity extends AppCompatActivity {
//define a veriable of type Executorservice
ExecutorService mExecutor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//define the number of threads you
// want to run parallel simultaneously
mExecutor= Executors.newFixedThreadPool(5);
}
Step3] Create the object of you runnable class . Then apply the execute() method on your executor variable and then pass that method your runnable object.
Dont forget to add a condition to execute any number of runnables on thread.
//users click on the button and starts the method
public void StartPool(View view) {
for (int i=0;i<10;i++){
Bike bike = new Bike(i+1);
//pass the executor a runnable to execute
mExecutor.execute(bike);
}
}
step4] lastly shutdown the executor in the ondestroy() method
//It is necessary to close an executor before activity is destroyed.
@Override
protected void onDestroy() {
mExecutor.shutdown();
super.onDestroy();
}
IMP : Depending upon your given number of fixed thread to run parrallely, the executor will run only that many threads at the same time , and as soon as any one thread ends,it executes the next /new thread if any.
----------------------------------------------------------------------------------------------------------------------------
FULL CODE FOR JAVA ACTIVITY
public class MainActivity extends AppCompatActivity {
//define a veriable of type Executorservice
ExecutorService mExecutor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//define the number of threads you
// want to run parallel simultaneously
mExecutor= Executors.newFixedThreadPool(5);
}
//users click on the button and starts the method
public void StartPool(View view) {
for (int i=0;i<10;i++){
Bike bike = new Bike(i+1);
//pass the executor a runnable to execute
mExecutor.execute(bike);
}
}
//It is necessary to close an executor before activity is destroyed.
@Override
protected void onDestroy() {
mExecutor.shutdown();
super.onDestroy();
}
}
Comments
Post a Comment