Friday, 2 December 2016

Java Tutorial : Java Threads (Thread names | How to set the Thread Name)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=EL0-llRn-qg&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java Threads (Thread names | How to set the Thread Name) 
HelloThread.java
public class HelloThread extends Thread
{

    public static void main(String args[])
    {
        HelloThread helloThread = new HelloThread();
        helloThread.start();
    }

    public void run()
    {
        /*
         * Returns this thread's name.
         */
        System.out.println("Thread Name = " + this.getName());
    }

}
Output
Thread Name = Thread-0

DisplayThread.java
public class DisplayThread extends Thread
{

    public DisplayThread(String threadName)
    {
        super(threadName);
    }

    public static void main(String[] args)
    {
        DisplayThread displayThread1 = new DisplayThread("DisplayThread_100");
        displayThread1.start();
        
        DisplayThread displayThread2 = new DisplayThread("DisplayThread_200");
        displayThread2.start();

    }

    public void run()
    {
        /*
         * Returns this thread's name.
         */
        System.out.println("Run by = " + this.getName());
    }
}
Output
Run by = DisplayThread_100
Run by = DisplayThread_200

HelloRunnable.java
/*
 * public Thread(Runnable target, String name)
 * 
 * Parameters:
 * -----------
 * 
 * target - the object whose run method is invoked when
 * this thread is started. If null, this thread's run
 * method is invoked.
 * 
 * name - the name of the new thread
 */

public class HelloRunnable implements Runnable
{

    public static void main(String args[])
    {
        HelloRunnable helloRunnable = new HelloRunnable();
        Thread thread = new Thread(helloRunnable, "HelloRunnableThread");
        thread.start();
        /*
         * Returns this thread's name.
         */
        System.out.println("Thread Name = " + thread.getName());
    }

    @Override
    public void run()
    {
        System.out.println("Hello from a thread!");
    }
}
Output
Thread Name = HelloRunnableThread
Hello from a thread!

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/Thread_names_app.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/5edb9b935a4ef60d2a672d03f038390cd08d4eb7/BasicJava/Thread_names_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