其他分享
首页 > 其他分享> > 继承Thread类 重新run方法

继承Thread类 重新run方法

作者:互联网

package com.com.duoxiancheng;

public class Test1 {
public static void main(String[] args) {//主线程
//1创建线程对象
Mythread mt = new Mythread();
//2调用start()方法启动一个线程
// mt.run();
mt.start();//继承Thread类 表示启动一个子线程 两个线程一起跑 if是mt.run 先跑完这个再继续主线程
for (int i = 0; i < 1000; i++) {
System.out.println("======我是主线程" + i);
}
//两个线程交替着跑

}
}


package com.com.duoxiancheng;

public class Mythread extends Thread{
@Override//重写run方法
public void run() {
//要把子线程执 行的内容写在run里
for (int i = 0; i <1000 ; i++) {
System.out.println("我是子线程" + i);
}
}
}

搜索

复制

标签:run,Thread,继承,mt,Mythread,线程,com,public
来源: https://www.cnblogs.com/wutonga/p/16281465.html