多线程 JAVA
Thread
不是抽象类,new Thread()
完全可以,只是默认无任务。
继承 Thread 类:通过继承
Thread
类,重写其run()
方法定义线程执行逻辑,直接调用start()
方法启动线程。class MyThread extends Thread {public void run() { ... } // 重写run() } // 使用:new MyThread().start();
严格来说 这个不是一个线程 只是一个普通类
实现 Runnable 接口:实现
Runnable
接口的run()
方法,将实例作为参数传入Thread
对象,再调用start()
。class MyRunnable implements Runnable {public void run() { ... } // 实现run() } // 使用:new Thread(new MyRunnable()).start();