Thread Implementation Using Runnable Interface
public class Threads2 implements Runnable { public static void main(String[] args) { Threads2 objThreads2 = new Threads2(); Thread t1 = new Thread(new Threads2()); t1.start(); } public void run() { System.out.println("Thread Started"); for(int i=0;i<=5;i++) { System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Thread Stopped"); } }
There are Four Constructors we can use in Runnable Method of Implementation of Threads.
Thread() – Calls Thread Version of run Method
Thread(Runnable Target)
Thread(Runnable Target, String Name)
Thread(String Name)
If we dont Pass the Instance of Class To thread Constrctor then it will call its own Run Method
Thread t = new Thread(new MyThread());