• Its recommended to learn about life cycle of Thread before you start programming on Thread.
  • Threads exists in different type of states.

  • Thread having below states.
  1. New State
  2. Ready State
  3. Running State
  4. Dead State
  5. Non Runnable States 

Life cycle of thread in java with diagram
Life cycle of a Thread

1.New State:

  • A thread has been created but not started yet. A thread will be started by calling its start() method.

2.Runnable State:

  • This state is also called ready to run stage also called queue. A thread starts in runnable state by calling start() method.
  • The Thread scheduler decides which thread runs and how long.

3.Running State:

  • If a Thread is executing that means Thread is in Running stage.

4.Dead State:

  • Once a Thread reached dead state it can not run again.

5. Non runnable States:

  • A Running Thread transit to one of the non runnable states, depending upon the circumstances.
  • A Thread remains non runnable until a special transition occurs.
  • A Thread does not go directly to the running state from non runnable state.
  • But transits first to runnable state.
  1. Sleeping: The Threas sleeps for specified amount of time.
  2. Blocked for I/O: The Thread waits for a blocking operation to complete.
  3. Blocked for join completion: The Thread waits for completion of another Thread.
  4. Waiting for notification: The Thread waits for notification another Thread.
  5. Blocked for lock acquisition: The Thread waits to acquire the lock of an object.
  • JVM executes the Thread based on their priority and scheduling.

Thread Scheduler:

  • Schedulers in JVM implementations usually employ one of these two Strategies.
  • Preemptive Scheduling
  • Time Sliced or Round robin Scheduling
  • Thread schedulers are implementation and platform independent, therefore how thread will scheduled is unpredictable

Thread priority:

  • JVM will assign a priority for every Thread created in it.
  • 0- will be the minimum priority
  • 5- will be the normal priority
  • 10- will be the maximum priority
  • To hold all these values Thread class has below three corresponding variables
  • public static final int MIN_PRIORITY
  • public static final int NORM_PRIORITY
  • public static final int MAX_PRIORITY 
  •  A thread inherits the priority of its parent Thread. The default priority of the every thread is normal priority 5, because main thread priority is 5.
  • We can set the priority of a thread by using setPriority(int priority) method
  • public final void setPriority(int priority)
  • public void getPriority();
  • User defined thread created with default name  Thread+<index>, where index is the integer number starts from 0.
  • The name of a thread can be change using setName(String name) method.
  • Get by using getName() method.
  • public final void setName(String name)
  • public final String getName().

Life cycle of thread in java program
  1. package com.instanceofjava;
  3. class UserThread{
  5. UserThread(){
  6.         super();
  7.  }
  9.  UserThread(String name){
  10.         UserThread(name);        
  11.  }
  13.  public void run()
  14.  {
  15.       System.out.println("thread started running..");
  16.  }
  18. public static void main(String [] args){ 

  19.   UserThread thread1 = new UserThread("Thread1");
  20.   UserThread thread2 = new UserThread("Thread2");        

  21.          System.out.println("Thread 1 initial name and priority");
  22.          System.out.println("name:"+thread1.getName());
  23.          System.out.println("priority:"+thread1.getPriority());
  25.       System.out.println("Thread 2 initial name and priority");
  26.       System.out.println("name:"+thread2.getName());
  27.       System.out.println("priority:"+thread2.getPriority());
  29.       thread1.setPriority(6);
  30.       thread2.setPriority(9);
  32.       System.out.println("Thread 1 initial name and priority");
  33.       System.out.println("name:"+thread1.getName());
  34.       System.out.println("priority:"+thread1.getPriority())
  36.       System.out.println("Thread 2 initial name and priority");
  37.       System.out.println("name:"+thread2.getName());
  38.       System.out.println("priority:"+thread2.getPriority());
  40.       thread1.start();
  41.       thread2.start();
  43.       for (int i = 0; i < 5; i++) {
  44.           System.out.println("main method i value:"+i);
  45.     }
  46. }
  47. }


  1. Thread 1 initial name and priority
  2. name:Thread1
  3. priority:5
  4. Thread 2 initial name and priority
  5. name:Thread2
  6. priority:5
  7. Thread 1 initial name and priority
  8. name:Thread1
  9. priority:6
  10. Thread 2 initial name and priority
  11. name:Thread2
  12. priority:9
  13. Thread1i:0
  14. Thread1i:1
  15. Thread1i:2
  16. Thread2i:0
  17. Thread1i:3
  18. Thread1i:4
  19. Thread2i:1
  20. Thread2i:2
  21. Thread2i:3
  22. Thread2i:4
  23. main method i value:0
  24. main method i value:1
  25. main method i value:2
  26. main method i value:3
  27. main method i value:4

Java programming interview questions

  1. Print prime numbers? 
  2. What happens if we place return statement in try catch blocks 
  3. Write a java program to convert binary to decimal 
  4. Java Program to convert Decimal to Binary
  5. Java program to restrict a class from creating not more than three objects
  6. Java basic interview programs on this keyword 
  7. Interfaces allows constructors? 
  8. Can we create static constructor in java 
  9. Super keyword interview questions java 
  10. Java interview questions on final keyword
  11. Can we create private constructor in java
  12. Java Program Find Second highest number in an integer array 
  13. Java interview programming questions on interfaces 
  14. Top 15 abstract class interview questions  
  15. Java interview Questions on main() method  
  16. Top 20 collection framework interview Questions
  17. Java Interview Program to find smallest and second smallest number in an array 
  18. Java Coding Interview programming Questions : Java Test on HashMap  
  19. Explain java data types with example programs 
  20. Constructor chaining in java with example programs 
  21. Swap two numbers without using third variable in java 
  22. Find sum of digits in java 
  23. How to create immutable class in java 
  24. AtomicInteger in java 
  25. Check Even or Odd without using modulus and division  
  26. String Reverse Without using String API 
  27. Find Biggest substring in between specified character
  28. Check string is palindrome or not?
  29. Reverse a number in java?
  30. Fibonacci series with Recursive?
  31. Fibonacci series without using Recursive?
  32. Sort the String using string API?
  33. Sort the String without using String API?
  34. what is the difference between method overloading and method overriding?
  35. How to find largest element in an array with index and value ?
  36. Sort integer array using bubble sort in java?
  37. Object Cloning in java example?
  38. Method Overriding in java?
  39. Program for create Singleton class?
  40. Print numbers in pyramid shape?
  41. Check armstrong number or not?
  42. Producer Consumer Problem?
  43. Remove duplicate elements from an array
  44. Convert Byte Array to String
  45. Print 1 to 10 without using loops
  46. Add 2 Matrices
  47. Multiply 2 Matrices
  48. How to Add elements to hash map and Display
  49. Sort ArrayList in descending order
  50. Sort Object Using Comparator
  51. Count Number of Occurrences of character in a String
  52. Can we Overload static methods in java
  53. Can we Override static methods in java 
  54. Can we call super class static methods from sub class 
  55. Explain return type in java 
  56. Can we call Sub class methods using super class object? 
  57. Can we Override private methods ? 
  58. Basic Programming Questions to Practice : Test your Skill
  59. Java programming interview questions on collections

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
Newer Post
Older Post

No comments

Leave a Reply

Select Menu