Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
Thread Priority in Android
Next article icon

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




Next Article
Thread Priority in Android

A

aashaypawar
Improve
Article Tags :
  • Operating Systems
  • Quizzes
  • Computer Science Quizzes
  • Java Quiz
  • Web Technologies
  • Kotlin
  • Write From Home
  • Android
  • DSA
  • Kotlin Android
  • Java-Android

Similar Reads

    Android Architecture
    Android architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern
    5 min read
    How Does Threading Work 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 respo
    6 min read
    POSIX Threads in OS
    POSIX Threads in OS :The POSIX thread libraries are a C/C++ thread API based on standards. It enables the creation of a new concurrent process flow. It works well on multi-processor or multi-core systems, where the process flow may be scheduled to execute on another processor, increasing speed throu
    4 min read
    Threads and its Types in Operating System
    A thread is a single sequence stream within a process. Threads have the same properties as the process so they are called lightweight processes. On single core processor, threads are are rapidly switched giving the illusion that they are executing in parallel. In multi-core systems, threads can exec
    8 min read
    Running User Interface Thread in Android using Kotlin
    User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, that to update an element or change its attributes in the application layout ie the front-end of the application, one can make use o
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences