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:
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.
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.
The below XML code is for the Layout.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Running User Interface Thread in Android using Kotlin
- Difference between Priority Inversion and Priority Inheritance
- Difference between Preemptive Priority based and Non-preemptive Priority based CPU scheduling algorithms
- Difference between User Level thread and Kernel Level thread
- Relationship between User level thread and Kernel level thread
- Why Kotlin will replace Java for Android App Development
- How to create project in Android Studio using Kotlin
- Android EditText in Kotlin
- Android LinearLayout in Kotlin
- Android RelativeLayout in Kotlin
- Android ListView in Kotlin
- Android FrameLayout in Kotlin
- Android TableLayout in Kotlin
- Android Toast in Kotlin
- Android Fade In/Out in Kotlin
- Android Slide Up/Down in Kotlin
- Android Animations in Kotlin
- Android progress notifications in Kotlin
- Kotlin Android Tutorial
- Difference between Java and Kotlin in Android with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.