[26]线程的匿名小问题

版权声明:嘤嘤嘤,小白的东西,大牛们应该看不上吧。 https://blog.csdn.net/qq_37384180/article/details/83999861
new Thread(new Runnbale(){
    @override
    public void run(){
       System.out.println("subThread");
    }
}){
    @override
    public void run(){
        System.out.println("Thread");
    }
}.start();

输出结果:

Thread 

 详细化:

class Demo{
    public static void main(String[] args){
        Test t=new Test();
        Thread t1=new Thread(t){
            @override
            public void run(){
                System.out.println("Thread");
            }
        };
        t1.start();
    }
}

class Test implements Runnable {
    private Runnable r;
    @override
    public void run(){
        if(r!=null){
            System.out.println("subThread");
        }
    }
    public void start(){
        run();
    }
}

将Thread再详细化

class Test implements Runnbale{
    private Runnable r;
    @override
    public void run(){
        if(r!=null){
            System.out.println("subThread");
        }
    }
    public void start(){
        run();
    }
}

class SubThread extends Thread{
    @override
    public void run(){
        System.out.println("Thread");
    }
}

class Demo{
    public static void main(String[] args){
       Test test=new Test();
       SubThread sub=new SubThread(test); 
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37384180/article/details/83999861