To check if the main thread is running you cannot make use of the Thread.isAlive() method : you may get the error "Cannot make a static reference to the non-static method isAlive() from the type Thread", as main is a static method thus can only access static methods.
To check if the main method thread is alive you can get the instance of the main thread by Thread.currentThread() static method.
File: ThreadAlive.javapackage com.code2care.examples.java;
public class ThreadAlive {
public static void main(String[] args) {
Thread t = Thread.currentThread();
System.out.println("Thread : Main Exited "
+ Thread.currentThread().isAlive());
}
}
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Install Gradle VS Code for Java Projects - Gradle
- Fix: Ubuntu (Linux) - bash: sudo: command not found error - Ubuntu
- The Android Virtual Device is currently running in an emulator and cannot be deleted - Android
- TextEdit: Disable Autocorrect Option (Mac) - MacOS
- Free and easy audio video screen recording using Microsoft PowerPoint - Microsoft
- Install Cygwin on Windows PC to learn Objective-C - Objective-C
- 28: Program to Lowercase a String in Python - Python-Programs
- Power BI error Something went wrong, unable to read the application metadata - Microsoft