Thread Priority in Kotlin & Android

Every thread in a process has a Priority. They are in the range of 1 to 10. Threads are scheduled according to their priorities with the help of a Thread Scheduler.
There can be 3 priority constant set for a Thread which are:

  • MIN_PRIORITY which equals to 1
  • MAX_PRIORITY which equals to 10
  • NORM_PRIORITY which is a default value and equals to 5

Below is a code to check the priorities of two threads.

Checking the current priorities of Threads:

filter_none

edit
close

play_arrow

link
brightness_4
code

val t1 = Thread(Runnable{
    // Some Activity
})
  
    val t2
    = Thread(Runnable{
        // Some Activity
    })
  
        println("${t1.priority} ${t2.priority}")

chevron_right


Output: 5 5

The output for both the threads are same as Default priority of a thread is 5.
Assigning new priorities to Threads:
Below the two threads are assigned different priorities.Thread t1 is assigned 1 and thread t2 is assigned 10. Since Thread t1 has more priority, it shall start first and the remaining threads according to the priority. The priority can be declared implicitly or explicitly.

filter_none

edit
close

play_arrow

link
brightness_4
code

val t1 = Thread(Runnable{
    // Some Activity
})
  
    val t2= Thread(Runnable{
        // Some Activity
    })
  
    t1.priority= 1 
    t2.priority = 10
  
    println("${t1.priority} ${t2.priority}")

chevron_right


Output : 1 10

The same can be implemented in an Android application, below is an example.
Example in Android:
Try running the below program in Android to check the priorities of two threads declared within the code. When the user clicks the button, the thread with more priority starts.



filter_none

edit
close

play_arrow

link
brightness_4
code

package com.example.gfg
  
    import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.TextView
  
    class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
  
                val tv = findViewById<TextView>(R.id.tv)
                val btn1= findViewById<Button>(R.id.btn1)
  
                val t1 = Thread(Runnable{
                tv.text = "t1 had more priority" })
  
                val t2= Thread(Runnable{
                tv.text = "t2 had more priority" })
  
                t1.priority= 1 
                t2.priority = 10
  
               btn1.setOnClickListener{
               when{
                t1.priority < t2.priority -> t1.start() t2.priority < t1.priority -> t2.start() else -> tv.text = 
                 "Both had same priority"
                }
            btn1.isEnabled = false
        }
    }
}

chevron_right


The below XML code is for the Layout.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/tv"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:gravity="center"
        android:textColor="@color/colorAccent"
        android:background="@color/colorPrimary"
        android:layout_centerInParent="true"
        android:textSize="30sp"
        />
  
  
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/tv"
        />
  
  
</RelativeLayout>

chevron_right


Output:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.