Working on multiple tasks at the same time is Multitasking. In the same way, multiple threads running at the same time in a machine is called Multi-Threading. Technically, a thread is a unit of a process. Multiple such threads combine to form a process. This means when a process is broken, the equivalent number of threads are available.
For example, Autocorrect is the process where the software looks for the mistakes in the current word being typed. Endlessly checking for the mistake and providing suggestions at the same time is an example of a Multi-Threaded process.
Sample Android App:
Let’s try to visualize Multi-Threading with the help of an Android App. In the below example, 3 Threads start at the same time on a button click and work concurrently.
Approach:
Step 1: Add the below code in activity_main.xml. Here we add three TextViews and a button.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < TextView android:id = "@+id/tv1" android:layout_width = "75sp" android:layout_height = "75sp" android:background = "@color/colorPrimary" android:textColor = "@color/colorAccent" android:gravity = "center" android:textSize = "10sp" android:layout_centerVertical = "true" android:layout_toLeftOf = "@id/tv2" /> < TextView android:id = "@+id/tv2" android:layout_width = "75sp" android:layout_height = "75sp" android:background = "@color/colorPrimary" android:textColor = "@color/colorAccent" android:gravity = "center" android:textSize = "10sp" android:layout_centerInParent = "true" /> < TextView android:id = "@+id/tv3" android:layout_width = "75sp" android:layout_height = "75sp" android:background = "@color/colorPrimary" android:textColor = "@color/colorAccent" android:gravity = "center" android:textSize = "10sp" android:layout_centerVertical = "true" android:layout_toRightOf = "@id/tv2" /> < Button android:id = "@+id/btnStart" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Start" android:layout_centerHorizontal = "true" android:layout_below = "@id/tv2" /> </ RelativeLayout > |
Step 2: Add the below code in MainActivity. Here, three threads are made, each thread keeps updating the respective TextView every second (declared in the code) when the button is clicked. These Threads keep running until the button is clicked again (i.e.”Stop”).
Java
class MainActivity : AppCompatActivity() { @SuppressLint ( "SetTextI18n" ) override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Assigning Layout elements val tv1 = findViewById<TextView>(R.id.tv1) val tv2 = findViewById<TextView>(R.id.tv2) val tv3 = findViewById<TextView>(R.id.tv3) val btn = findViewById<Button>(R.id.btnStart) // Boolean for Button (initially False) var boolbtn = false // Button onClick action btn.setOnClickListener { // Button (True) boolbtn = !boolbtn // Case where Button is False if (!boolbtn) { tv1.text = "Stopped1" tv2.text = "Stopped2" tv3.text = "Stopped3" btn.text = "Start" } // Case where Threads are running else { // Setting the button text as "Stop" btn.text = "Stop" // Thread 1 Thread(Runnable { // Runs only when Button is True while (boolbtn) { runOnUiThread { tv1.text = "Started1" } Thread.sleep( 1000 ) runOnUiThread { tv1.text = "Activity1" } Thread.sleep( 1000 ) } }).start() // Thread 2 Thread(Runnable { // Runs only when Button is // True while (boolbtn) { runOnUiThread { tv2.text = "Started2" } Thread.sleep( 1000 ) runOnUiThread { tv2.text = "Activity2" } Thread.sleep( 1000 ) } }).start() // Thread 3 Thread(Runnable { // Runs only when Button is // True while (boolbtn) { runOnUiThread { tv3.text = "Started3" } Thread.sleep( 1000 ) runOnUiThread { tv3.text = "Activity3" } Thread.sleep( 1000 ) } }) .start() } } } } |
Output:
Multi-Threading concept applies to a large number of day to day applications on various machines. However, there are drawbacks to the same. At the beginner level, one can only think of the load on the machine or increased complexity. But at a greater level, multiple scenarios such as complex testing, unpredictable results, deadlock, starvation, etc are expected.