一、重写Runnable的run()方法
a.覆写Runnable接口实现多线程可以避免单继承局限
b.当子类实现Runnable接口,此时子类和Thread的代理模式(子类负责真是业务的操作,thread负责资源调度与线程创建辅助真实业务。
在执行过程中start()和run()的区别
start()方法来启动线程,是真正实现的多线程运行,这个时候无需等待run方法体执行完毕,就可以直接执行之后的线程
run() 方法当做普通的方法调用,程序还是要按顺序执行,要等待run()方法体内的数据执行完毕后,才可执行之后的代码
public class MyThread implements Runnable{
public static int count=20;
public void run() {
while(count>0) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-当前剩余票数:"+count--);
}
}
public static void main(String[] args) {
MyThread Thread1=new MyThread();
Thread mThread1=new Thread(Thread1,"线程1");
Thread mThread2=ne