- 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.
- New State
- Ready State
- Running State
- Dead State
- Non Runnable States
Life cycle of thread in java with diagram
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.
- Sleeping: The Threas sleeps for specified amount of time.
- Blocked for I/O: The Thread waits for a blocking operation to complete.
- Blocked for join completion: The Thread waits for completion of another Thread.
- Waiting for notification: The Thread waits for notification another Thread.
- 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().
- package com.instanceofjava;
- class UserThread{
- UserThread(){
- super();
- }
- UserThread(String name){
- UserThread(name);
- }
- public void run()
- {
- System.out.println("thread started running..");
- }
- public static void main(String [] args){
- UserThread thread1 = new UserThread("Thread1");
- UserThread thread2 = new UserThread("Thread2");
- System.out.println("Thread 1 initial name and priority");
- System.out.println("name:"+thread1.getName());
- System.out.println("priority:"+thread1.getPriority());
- System.out.println("Thread 2 initial name and priority");
- System.out.println("name:"+thread2.getName());
- System.out.println("priority:"+thread2.getPriority());
- thread1.setPriority(6);
- thread2.setPriority(9);
- System.out.println("Thread 1 initial name and priority");
- System.out.println("name:"+thread1.getName());
- System.out.println("priority:"+thread1.getPriority())
- System.out.println("Thread 2 initial name and priority");
- System.out.println("name:"+thread2.getName());
- System.out.println("priority:"+thread2.getPriority());
- thread1.start();
- thread2.start();
- for (int i = 0; i < 5; i++) {
- System.out.println("main method i value:"+i);
- }
- }
- }
Output:
- Thread 1 initial name and priority
- name:Thread1
- priority:5
- Thread 2 initial name and priority
- name:Thread2
- priority:5
- Thread 1 initial name and priority
- name:Thread1
- priority:6
- Thread 2 initial name and priority
- name:Thread2
- priority:9
- Thread1i:0
- Thread1i:1
- Thread1i:2
- Thread2i:0
- Thread1i:3
- Thread1i:4
- Thread2i:1
- Thread2i:2
- Thread2i:3
- Thread2i:4
- main method i value:0
- main method i value:1
- main method i value:2
- main method i value:3
- main method i value:4
Java programming interview questions
- Print prime numbers?
- What happens if we place return statement in try catch blocks
- Write a java program to convert binary to decimal
- Java Program to convert Decimal to Binary
- Is it possible to print message without using System.out.println()
- Java program to restrict a class from creating not more than three objects
- Java basic interview programs on this keyword
- Java Program to Sort elements of Java ArrayList Example
- Interfaces allows constructors?
- Can we create static constructor in java
- Super keyword interview questions java
- Java interview questions on final keyword
- Can we create private constructor in java
- Java Program Find Second highest number in an integer array
- Java interview programming questions on interfaces
- Top 15 abstract class interview questions
- Java interview Questions on main() method
- Sort employee object by id in descending order using comparable and TreesSet
- Top 20 collection framework interview Questions
- Java Interview Program to find smallest and second smallest number in an array
- Java Coding Interview programming Questions : Java Test on HashMap
- Explain java data types with example programs
- How to check internet connection using java
- Constructor chaining in java with example programs
- Top 10 Interview Programs and questions on method overriding in java
- Swap two numbers without using third variable in java
- Find sum of digits in java
- How to create immutable class in java
- AtomicInteger in java
- Check Even or Odd without using modulus and division
- String Reverse Without using String API
- Find Biggest substring in between specified character
- Check string is palindrome or not?
- Reverse a number in java?
- Fibonacci series with Recursive?
- Fibonacci series without using Recursive?
- Sort the String using string API?
- Sort the String without using String API?
- what is the difference between method overloading and method overriding?
- How to find largest element in an array with index and value ?
- Sort integer array using bubble sort in java?
- Object Cloning in java example?
- Method Overriding in java?
- Program for create Singleton class?
- Print numbers in pyramid shape?
- Check armstrong number or not?
- Producer Consumer Problem?
- Remove duplicate elements from an array
- Convert Byte Array to String
- Print 1 to 10 without using loops
- Add 2 Matrices
- Multiply 2 Matrices
- How to Add elements to hash map and Display
- Sort ArrayList in descending order
- Sort Object Using Comparator
- Count Number of Occurrences of character in a String
- Can we Overload static methods in java
- Can we Override static methods in java
- Can we call super class static methods from sub class
- Explain return type in java
- Can we call Sub class methods using super class object?
- Can we Override private methods ?
- Basic Programming Questions to Practice : Test your Skill
- Java programming interview questions on collections
No comments