Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Thread and instantiating thread in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.19k
    Comment on it

    In this blog we will learn about importance of Thread class in java and instantiating thread. Every java program consists at least one thread i.e. the main thread. The Java Virtual Machine always creates a main thread and calls the main method in the java program when ever we start our java program.


    Thread and instantiating Thread

    A thread is a single point of execution. A thread can not run on it's own rather it runs within a program.

    In Java there are two ways in which we can create thread mentioned as below:

    1- We can create a thread by creating an object of a Thread class.

    2- Or we can implement an interface named as Runnable.


    Thread class:

    The “Thread” class provide methods that help to perform different operations on a thread. Below are some commonly used constructors that are used to create an object of a thread.

    Thread() : It is a default constructor

    Thread(String name) : A constructor that passed a name of a thread.

    Thread(Runnable r) : Here the constructor will create thread from the object of a runnable class.

    Thread(Runnable r,String name) : Here the constructor will create thread from the object of a runnable class with name of the thread as passed in the form of second argument


    Example of thread execution using extend method:

    import java.lang.Thread;
    
    class Mythread extends Thread{
    	
    	public void run(){
    	 System.out.println("My demo thread started running....");	
    	}
    	
    	public static void main(String args[]){	
    		Mythread demo = new Mythread();
    		demo.start();
    	}
    	}
    
    
    // Compiling the sorce code
    username@machinename:/path/of/the/sorucefile/with_extention_java$ javac Mythread.java 
    
    // Running the program
    username@machinename:/path/of/the/sorucefile/with/extention/java$ java Mythread // running the program
    
    Output:
    
    My demo thread started running....

    Instantiating a thread while extending a “Thread” class:

     

    If we choose to extend Thread class : Then Instantiation would be as below

    DemoThread thread = new DemoThread();

    Using Runnable Interface:

    First we implement the "Runnable" Interface in a class whose instance have the need to be executed by a thread. The Runnable interface consists of a method called run(). We need to remember some important things about run() method.

    - The run( ) method can call other methods.

    - It can use other classes.

    - It can declare variables, as the main thread do.

    - This thread ends on return.

    In the below example a class “ MythreadRunnable” implements the Runnable interface and the Runnable object of this class provides run() method to the thread.

    class MythreadRunnable implements Runnable{
    	
    	public void run(){
    	 System.out.println("My demo thread started running example of implementing runnable interface....");	
    	}
    	
    	public static void main(String args[]){
    		
    		MythreadRunnable demorunnable = new MythreadRunnable();
    		Thread demo = new Thread(demorunnable);
    		demo.start();
    	}
    	
    }
    
    
    
    // Compiling the sorce code
    username@machinename:/path/of/the/sorucefile/with_extention_java$ javac MythreadRunnable.java 
    
    // Running the program
    username@machinename:/path/of/the/sorucefile/with/extention/java$ java MythreadRunnable
    
    Output:
    
    My demo thread started running example of implementing runnable interface....

    If we implement “Runnable” interface, instantiation is done as below:

    First we instantiate our Runnable class: 
    MythreadRunnable demorunnable = new MythreadRunnable ();
    Thread thread = new Thread(demorunnable); //We passed our demorunnable object to the Thread

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: