Blog purpose for android basic example for android app developer. any query please my contact

Monday, 6 March 2023

what is thread in android

 what is thread in android


When an application is launched in Android, it creates the primary thread of execution, referred to as the “main” thread. Most thread is liable for dispatching events to the acceptable interface widgets also as communicating with components from the Android UI toolkit. To keep your application responsive, it’s essential to avoid using the most thread to perform any operation which will find yourself keeping it blocked.


When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread).


What is thread in Android with example?

A thread is a lightweight sub-process, it going to do background operations without interrupt to ui. This example demonstrate about How to create a thread in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.



public class MainActivity Activity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

// Adding Task to the List (Async)

new MyTask().execute(url);

}


private class MyTask

extends AsyncTask<String, Void, String> {


@Override

protected String doInBackground(String... params)

{

String url = params[0];

return doSomeWork(url);

}


@Override

protected void onPostExecute(String result)

{

super.onPostExecute(result);

// do something with the result

}

}

}



No comments:

Post a Comment