Multithreading Example – Part 2 use of Thread class

A simple example to illustrate the multithreading:

import java.util.*;

class A extends Thread{
Scanner in=new Scanner(System.in);
    public void run(){
        for(int i=1; i            System.out.println(“Please provide an input”);
            System.out.println(“Got :”+in.next());
        }
    }
}

class B extends Thread{
    String[] args;
    B(String[] args){
        this.args=args;
    }
    public void run(){
        for(int i=0; i            System.out.println(Thread.currentThread().getName()+” :: “+args[i]);
        }
    }
}
class C extends Thread{
    public void run(){
        for(int i=0; i            System.out.println(Thread.currentThread().getName()+” : “+i);
        }
    }
}
class G{
public static void main(String[] args){
    A a=new A();
    B b=new B(args);
    C c=new C();
   
    Thread t1=new Thread(a, “First”);
    Thread t2=new Thread(b, “Second”);
    Thread t3=new Thread(c, “Third”);
   
    t1.start();
    t2.start();
    t3.start();
}
}
5>5>5>

Save with a suitable name, compile using command prompt  and execute…