2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

how to create a thread in java.

how to create my custome thread in java.
userimage

To create a custom thread you can use


1st way: Extend the Thread class


public class CustomThread extends Thread{ 
    public void run(){ 
        System.out.println("thread is running..."); 
    } 
    public static void main(String args[]){ 
        CustomThread customThread=new CustomThread(); 
        customThread.start(); 
    } 


2nd way: Implement Runnable Interface:


public class CustomThread implements Runnable{ 
    public void run(){ 
        System.out.println("thread is running..."); 
    } 
    public static void main(String args[]){ 
        CustomThread customThread=new CustomThread(); 
        Thread thread1 =new Thread(customThread); 
        thread1.start(); 
    } 

Answer is