
//继承Thread
public class MyThread extends Thread {
    @Override
    public void run() {
        for(int i = 0; i < 100; i++) {
            System.out.println("MyThread:" + i);
        }
    }
}
//实现Runnable
public class MyRunnableImpl implements Runnable {
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("MyRunnableImpl:" + i);
        }
    }
}
//测试类
public class Test {
    public static void main(String[] args) {
        Thread thread1 = new MyThread();
        Thread thread2 = new Thread(new MyRunnableImpl());
        thread1.start();
        thread2.start();
    }
}
//输出结果示例
...
MyThread:23
MyThread:24
MyThread:25
MyRunnableImpl:0
MyThread:26
MyRunnableImpl:1
MyThread:27
MyRunnableImpl:2
...//继承Threadpublic class MyThread extends Thread {        public void run() {        for(int i = 0; i < 100; i++) {            System.out.println("MyThread:" + i);        }    }}//实现Runnablepublic class MyRunnableImpl implements Runnable {    public void run() {        for (int i = 0; i < 100; i++) {            System.out.println("MyRunnableImpl:" + i);        }    }}//测试类public class Test {    public static void main(String[] args) {        Thread thread1 = new MyThread();        Thread thread2 = new Thread(new MyRunnableImpl());        thread1.start();        thread2.start();    }}//输出结果示例...MyThread:23MyThread:24MyThread:25MyRunnableImpl:0MyThread:26MyRunnableImpl:1MyThread:27MyRunnableImpl:2...public class Test {
    public static void main(String[] args) {
        //Thread
        Thread thread1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("anonymous thread:" + i);
                }
            }
        };
        //Runnable
        Thread thread2 = new Thread(new Runnable() {
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("anonymous runnable:" + i);
                }
            }
        });
        thread1.start();
        thread2.start();
    }
}public class Test {    public static void main(String[] args) {        //Thread        Thread thread1 = new Thread() {                        public void run() {                for (int i = 0; i < 100; i++) {                    System.out.println("anonymous thread:" + i);                }            }        };        //Runnable        Thread thread2 = new Thread(new Runnable() {            public void run() {                for (int i = 0; i < 100; i++) {                    System.out.println("anonymous runnable:" + i);                }            }        });        thread1.start();        thread2.start();    }}
原文:https://www.cnblogs.com/deng-cc/p/9461621.html