继承Thread类实现多线程
该方法是继承java.lang包下的Thread类,覆写这个类下面的run()方法,在run()方法中实现运行在线程上的代码。
public class MyThread {
public static void main(String[] args) {
MyThread1 myThread1 = new MyThread1("thread1");//创建线程MyThread的线程对象
MyThread1 myThread2 = new MyThread1("thread2");
MyThread1 myThread3 = new MyThread1("thread3");
myThread1.start();//开启线程
myThread2.start();
myThread3.start();
}
}
class MyThread1 extends Thread{
//线程主体
private String title;
public MyThread1(String title){
this.title= title;
}
public void run() {
//所有的线程从此处开始执行
for (int i = 0; i