Home » Android » Android Applications » How to create Runnable Thread in Android JAVA ?

How to create Runnable Thread in Android JAVA ?

Runnable in android / JAVA is an interface which needs to be implemented if we want run some thread and execute some code in that thread. Runnable is normally used to implemented multithreaded program.

In below simple example, we have implemented a Runnable interface “helloTask” which contains the implementation of “run” method which is executed when the thread in started.



public class HelloActivity extends Activity {
	static final String TAG = "HelloWorld";

 Runnable helloTask = new Runnable() {
	@Override
	public void run() {
		Log.d(TAG, "Started hello Task");
	}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	
        Thread thread = new Thread(helloTask);
	thread.start();
}

}

You can copy this code, and open the HelloActivity from application, then above code runs the thread and executes helloTask run method which can be seen from logcat as,

$ adb logcat | grep HelloWorld

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment