通过继承Thread类,实现多线程,这种方式有一定的局限性,因为,Java中只支持单继承,一个类一旦继承了某个父类,就无法再继承Thread类比如,学生类Student继承了Person类,就无法通过继承Thread类,创建线程
当通过Thread类的Runnabletarget构造方法,创建线程对象时,只需为该方法传递一个实现了Runnable接口的实例对象,这样,创建的线程将调用实现了Runnable接口中的,run()方法作为运行代码,而不需要调用Thread类中的run()方法示例,Example0java
public class Example03{
public static void main(String[] args){
// 创建MyThread的实例对象
// 创建线程对象
// 开启线程,执行线程中的run()方法
MyThread myThread=new MyThread();
Thread thread=new Thread(myThread);
thread.start();
while(true){
System.out.println('main()方法在运行');
}
}
}
class MyThread implements Runnable{
// 线程的代码段,当调用start()方法时,线程从此处开始执行
public void run(){
while(true){
System.out.println('MyThread类的run()方法在运行');
}
}
}
文章为作者独立观点,不代表股票交易接口观点