Open In App

Thread Priority in Android

Last Updated : 18 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

1. Checking the current priorities of Threads

Kotlin
val t1 = Thread(Runnable{
    // Some Activity
})

val t2 = Thread(Runnable{
    // Some Activity
})

println("${t1.priority} ${t2.priority}")

Output:

5 5

The output for both the threads are same as Default priority of a thread is 5.

2. 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.

Kotlin
val t1 = Thread(Runnable{
    // Some Activity
})

val t2= Thread(Runnable{
    // Some Activity
})

t1.priority= 1 
t2.priority = 10

println("${t1.priority} ${t2.priority}")

Output :

1 10

The same can be implemented in an Android application, below is an example.

Example of Android Application

The Application can be divided into two sections Kotlin Program and xml code.

1. XML Layout

The below XML code is for the Layout.

activity_main.xml:

XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="300sp"
        android:layout_height="100sp"
        android:gravity="center"
        android:textColor="@color/black"
        android:layout_marginBottom="30dp"
        android:layout_centerInParent="true"
        android:textSize="25sp"
        />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go"
        android:backgroundTint="@android:color/holo_green_light"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/tv"
        />

</RelativeLayout>

Layout Design:

Layout


2. MainActivity Program

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.

MainActivity File:

Java
package com.gfg.thread_priority_java;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private Button btn1;
    private Thread thread1;
    private Thread thread2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = findViewById(R.id.tv);
        btn1 = findViewById(R.id.btn1);

        // Create thread1 with a Runnable
        thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                // Update the TextView on the UI thread
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv.setText("Thread 1 had more Priority");
                    }
                });
            }
        });

        // Create thread2 with a Runnable
        thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                // Update the TextView on the UI thread
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv.setText("Thread 2 had more Priority");
                    }
                });
            }
        });

        // Set priorities (1 is MIN_PRIORITY, 10 is MAX_PRIORITY)
        thread1.setPriority(Thread.MIN_PRIORITY);
        thread2.setPriority(Thread.MAX_PRIORITY -1);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Check priorities and start the appropriate thread
                if (thread1.getPriority() < thread2.getPriority()) {
                    thread2.start();
                    thread1.start();
                } else if (thread2.getPriority() < thread1.getPriority()) {
                    thread1.start();
                    thread2.start();
                } else {
                    tv.setText("Both had same Priority");
                }

                btn1.setEnabled(false);
            }
        });
    }
}
Kotlin
package com.gfg.thread_priority

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 thread1 = Thread(Runnable{ tv.text = "Thread 1 had more Priority" })
        val thread2 = Thread(Runnable{ tv.text = "Thread 2 had more Priority" })

        thread1.priority= 1
        thread2.priority = 9

        btn1.setOnClickListener{
            when{
                thread1.priority < thread2.priority -> thread1.start()
                thread2.priority < thread1.priority -> thread2.start()
                else -> tv.text = "Both had same Priority"
            }

            btn1.isEnabled = false
        }
    }
}

Output:

Output





Similar Reads