Threads In JAVA

 Creating a Thread

In the most general sense, you create a thread by instantiating an object of type Thread. Java defines two ways in which this can be accomplished: 

• You can implement the Runnable interface. 

• You can extend the Thread class, itself. 

Implementing Runnable

 The easiest way to create a thread is to create a class that implements the Runnable interface. Runnable abstracts a unit of executable code. You can construct a thread on any object that implements Runnable. To implement Runnable, a class need only implement a single method called run( ), which is declared like this: 

public void run( )  

Inside run( ), you will define the code that constitutes the new thread. It is important to understand that run( ) can call other methods, use other classes, and declare variables, just like the main thread can. The only difference is that run( ) establishes the entry point for another, concurrent thread of execution within your program. This thread will end when run( ) returns. 

 After you create a class that implements Runnable, you will instantiate an object of type Thread from within that class. Thread defines several constructors. 

Thread(Runnable threadOb, String threadName)

In this constructor, threadOb is an instance of a class that implements the Runnable interface. This defines where execution of the thread will begin. The name of the new thread is specified by threadName. 

After the new thread is created, it will not start running until you call its start( ) method, which is declared within Thread. In essence, start( ) initiates a call to run( ). The start( ) method is shown here: void start( )  

Example(Source Code):

class NewThread implements Runnable{
    Thread t;
    NewThread(){
        t = new Thread(this, "Demo Thread");
        System.out.println("Child Thread: "+t);
    }
    public void run(){
        try{
            for(int i=5;i>0;i--){
                System.out.println("Child Thread:"+i);
                Thread.sleep(500);
            }
        }
        catch(InterruptedException e){
            System.out.println("Child Interrupted");
        }
        System.out.println("Exiting child thread");
    }
}
class Main{
    public static void main(String[] args){
        NewThread nt = new NewThread();
        nt.t.start();
        try{
            for(int i=5; i<0;i--){
                System.out.println("Main Thread:"+i);
                Thread.sleep(1000);
            }
        }
        catch(InterruptedException e){
            System.out.println("Main Thread Interrupted");
        }
        System.out.println("Main Thread Exiting");
    }
}

Output:

Child Thread: Thread[Demo Thread,5,main]
Main Thread Exiting
Child Thread:5
Child Thread:4
Child Thread:3
Child Thread:2
Child Thread:1
Exiting child thread
 

 Extending Thread

The second way to create a thread is to create a new class that extends Thread, and then to create an instance of that class. The extending class must override the run( ) method, which is the entry point for the new thread. As before, a call to start( ) begins execution of the new thread.  

Example(Source Code):

class NewThread extends Thread{
    NewThread(){
        super("demo");
        System.out.println("Child thread"+this);
    }
    public void run(){
        try{
            for(int i=5; i>0; i--){
                System.out.println("Child Thread"+i);
                Thread.sleep(500);
            }
        }
        catch(InterruptedException e){
            System.out.println("Child Interrupted");
        }
        System.out.println("Exiting Child Thread");
    }
}
class Main{
    public static void main(String[] args){
        NewThread nt = new NewThread();
        nt.start();
        try{
            for(int i=5; i<0; i--)
            System.out.println("Main Thread"+i);
            Thread.sleep(1000);
        }
        catch(InterruptedException e){
            System.out.println("Main Thread Interrupted");
        }
        System.out.println("Main Thread Exiting");
    }
}

Output:

Child threadThread[demo,5,main]
Child Thread5
Child Thread4
Main Thread Exiting
Child Thread3
Child Thread2
Child Thread1
Exiting Child Thread

Comments

Popular posts from this blog

THREE LEVELS OF DATA INDEPENDENCE

Python Syntax, Comments, Variables, Indentation

Python-HackerRank Problem List Comprehensions