Saturday, 10 June 2017

Java Tutorial: Java Threads (How to check Thread is alive or not) ~ foundjava


Click here to watch in Youtube :
MyRunnable.java
public class MyRunnable implements Runnable
{

    public void run()
    {
        Thread t = Thread.currentThread();
        // tests if this thread is alive
        System.out.println(t.getName()+" - isAlive = " + t.isAlive());
    }
}
ThreadDemo.java
public class ThreadDemo
{

    public static void main(String args[]) throws InterruptedException
    {
        Thread t = new Thread(new MyRunnable());
        
        // this will call run() function
        t.start();
        
        // waits for this thread to die
        t.join();
        
        // tests if this thread is alive
        System.out.println(t.getName()+" - isAlive after join = " + t.isAlive());
    }
}
Output
Thread-0 - isAlive = true
Thread-0 - isAlive after join = false

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment