
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
isAlive() method of Thread Class in Java programming
The isAlive function − It is used to check if a thread is alive or not. Alive refers to a thread that has begun but not been terminated yet. When the run method is called, the thread operates for a specific period of time after which it stops executing.
Syntax
final Boolean isAlive()
The above returns true if the thread on which the function is called is running and hasn’t been terminated yet. It returns false otherwise.
Let us see an example −
Example
public class Demo extends Thread{ public void run(){ System.out.println("sample "); try{ Thread.sleep(25); } catch (InterruptedException ie){ } System.out.println("only "); } public static void main(String[] args){ Demo my_obj_1 = new Demo(); Demo my_obj_2 = new Demo(); my_obj_1.start(); System.out.println("The first object has been created and started"); my_obj_2.start(); System.out.println("The first object has been created and started"); System.out.println(my_obj_1.isAlive()); System.out.println("The isAlive function on first object has been called"); System.out.println(my_obj_2.isAlive()); System.out.println("The isAlive function on second object has been called"); } }
Output
The first object has been created and started sample The first object has been created and started sample true The isAlive function on first object has been called true The isAlive function on second object has been called only only
A class named Demo extends the Thread class. Here, a ‘run’ function is defined wherein a try catch block is defined. Here, in the try block, the sleep function is called and the catch block is left empty. In the main function, two instances of the Demo object are created. The first object is stated and it is checked whether it is running or basically in runnable state using the ‘isAlive’ function. The same is done with the second object as well.
- Related Articles
- How to use isAlive() method of Thread class in Java?
- The join() method of Thread Class in Java
- Object class in java programming
- CopyOnWriteArrayList Class in Java programming
- PriorityQueue Class in Java Programming
- What is the Thread class in Java?
- Math class methods in Java Programming
- How to make a class thread-safe in Java?
- Difference Between Thread Class and Runnable Interface in Java
- Properties of the Thread Class
- Methods of the Thread Class
- Method of Object class in Java
- How to create a thread by using anonymous class in Java?
- The indexOf() method of CopyOnWriteArrayList class in Java
- The addAll() method of AbstractList class in Java
