Sunday, 11 June 2017

Java Tutorial : Java Threads (How to create a thread in java | Runnable interface in java) ~ foundjava


Click here to watch in Youtube :


Click the below Image to Enlarge

Java Tutorial : Java Threads (How to create a thread in java | Runnable interface in java) 
Java Tutorial : Java Threads (How to create a thread in java | Runnable interface in java) 
 DisplayRunnable.java 
/*
 * If you are not extending the Thread class,your class
 * object would not be treated as a thread object.So you
 * need to explicitly create Thread class object.We are
 * passing the object of your class that implements
 * Runnable so that your class run() method may execute.
 */
public class DisplayRunnable implements Runnable
{
    public static void main(String args[])
    {
        DisplayRunnable displayRunnable = new DisplayRunnable();
        Thread thread = new Thread(displayRunnable);
        thread.start();
    }

    /*
     * When an object implementing interface Runnable is
     * used to create a thread, starting the thread causes
     * the object's run method to be called in that
     * separately executing thread.
     */
    @Override
    public void run()
    {
        System.out.println("Hello Peter..");
    }
}
Output
Hello Peter..

Refer: 
https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment