When Is a Java Thread Alive

When is a Java thread alive?

According to the Javadoc you mentionned:

A thread is alive if it has been started and has not yet died.

A thread "starts" when its start() method is invoked and "dies" at the end of its run() method, or when stop() (now deprecated) is invoked. So yes, a thread is "alive" when its run() method is still ongoing, but it is also "alive" in the time window between the invocation of start() and the implicit invocation of the run() method by the JVM.

You can also check the Thread.getState() and interesting information about Thread States suggested by @Marou Maroun.

I am also following his suggestion warning you that a Thread can end prematurely in case an Exception is thrown that propagates beyond run. The Thread would not be alive anymore in that case.

EDIT: As suggested by @zakkak, the thread can be considered alive even though the run() method did not start yet. In case you want to have proper control on when it will be invoked, use the ScheduledExecutorService, specifically the schedule() method which gives you more precise execution schedule.

How long a thread will be alive in java?

A thread created and started exactly as you describe will be alive only for as long as the empty Thread.run() method takes to do nothing and return. When the thread terminates, the t.isAlive() function will return false.

Normally, a thread does something useful and will be alive for as long as the run() method has not returned.

When is really alive a thread in java?

It's still alive, just not running.

From the documentation:

Tests if this thread is alive. A thread is alive if it has been started and has not yet died.

A thread which is just sleeping or waiting hasn't died (it hasn't exited its run method, either normally or abruptly), so isAlive will return true.

Of course it's very easy to test this:

public class Test {
public static void main(String[] args) throws Exception {
Runnable runnable = new Runnable() {
@Override public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
};

Thread thread = new Thread(runnable);
thread.start();
// Give the new thread plenty of time to really start
Thread.sleep(5000);
System.out.println(thread.isAlive());
}
}

How to keep a thread alive forever until JVM is killed?

Just adding an infinite loop should do the trick

public void run() {
while(true){
String event = sc.nextLine();
try {
queue.put(event); // thread will block here
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Can this thread be alive and how to use java.lang.Thread.join() method

Notice that although the (JDK 7) documentation for Thread.join(long) says

Waits at most millis milliseconds for this thread to die.

It also continues with

This implementation uses a loop of this.wait calls conditioned on this.isAlive.

And Thread.isAlive says:

A thread is alive if it has been started and has not yet died.

So with your code:

  • When calling t1.isAlive(), it is always false since you joined after starting the thread (from the constructor).
  • When calling t2.isAlive(), it is most of the time true since
    • you joined before starting the thread (in the constructor), which returns immediately
    • the thread has just started (in the constructor)

So to answer your question

It seems it can not be but could one of the t1 be alive ?

For t1, no, as long as you wait more than the time it takes it to run. For t2, yes.

What happens to the main application thread?

The main thread in a Java program dies when control reaches the end of main().

In the program architecture that you describe, the main thread already has created the EDT and maybe other threads by that point, and those other threads keep the JVM alive.

Every thread in Java is either a daemon thread, or a non-daemon thread. The latter is the default case for a new Thread. You have to call t.setDaemon(true) if you want a daemon.

A JVM will keep running as long as there is at least one non-daemon thread still alive.

What is the main thread used / usable for?

It is usable for any purpose that you would use any other thread for. It executes whatever code you write for it to execute.



Related Topics



Leave a reply



Submit