how to override thread.start() method in java?

前端 未结 10 1847
情歌与酒
情歌与酒 2020-12-03 02:07

I need to implement thread.start() method in my java code. Please let me know through an example of overriding of thread.start() method and how it works?

10条回答
  •  孤街浪徒
    2020-12-03 02:58

    We can override start/run method of Thread class because it is not final. But it is not recommended to override start() method

    class Bishal extends Thread { 
    public void start() 
        { 
            System.out.println("Start Method"); 
        } 
    public void run() 
        { 
            System.out.println("Run Method"); 
        } 
    } class Main{ 
    public static void main(String[] args) 
        { 
            Bishal thread = new Bishal(); 
            thread.start(); 
            System.out.println("Main Method"); 
        } 
    } 
    

    when we are calling start() method by an object of Bishal class, then any thread won’t be created and all the functions are done by main thread only.

提交回复
热议问题