匿名内部类实现线程程序的三种方式

/*
 * 使用匿名内部类,实现多线程程序
 * 匿名内部类的前提:继承或者接口实现
 * new 父类或者接口(){
 *         重写抽象方法
 * }
 */
public class ThreadDemo {
    public static void main(String[] args) {
        //继承方式 XXX extends     Thread{public void run(){}}
        //方式一
        new Thread(){
            public void run(){
                System.out.println("!!!");
            }
        }.start();
        
        //方式二
        //实现接口方式 XXX implements Runnable{public void run(){}}
        Runnable r= new Runnable() {
            public void run() {
                System.out.println("###");
            }
        };
        Thread t1=new Thread(r);
        t1.start();
        
        //方式三
        new Thread(new Runnable() {
            public void run() {
                System.out.println("@@@");
            }
        }).start();
        
    }
}

猜你喜欢

转载自blog.csdn.net/summoxj/article/details/80830733