-
继承Thread类,对该类进行new一个实例,对实例调用start方法,重写run方法。
缺点:单继承,无法继承
public class myThread extends Thread {public static void main(String[] args) {myThread myThread = new myThread();myThread.run();}@Overridepublic void run() {System.out.println("hello world");} }
-
实现Runnable接口
public class myThread implements Runnable {public static void main(String[] args) {Thread thread = new Thread(new myThread());thread.start();}@Overridepublic void run() {System.out.println("hello world");} }
其实Thread的底层是实现了Runnable接口的:
当然还有其他创建线程的方法,如创建线程池等。