Friday, 20 January 2017

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

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:
https://sites.google.com/site/ramj2eev1/home/javabasics/ThreadDemo_isAlive_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ThreadDemo_isAlive_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/4a03b5014bb513b61dfa414428ea9747b1421899/BasicJava/ThreadDemo_isAlive_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge