第一种:继承Thread类创建线程
public class MyThread1 extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("thread1:"+i);
}
}
}
public class MyRunnable1 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Runnable1:"+i);
}
}
}
在主方法实现效果
public class ThreadTest{
public static void main(String[] args) throws InterruptedException {
/* //继承Thread类创建线程
MyThread1 t1 = new MyThread1();
MyThread2 t2 = new MyThread2();
t1.start();
t2.start();*/
//实现Runnable接口创建线程
Thread r1 = new Thread(new MyRunnable1());
Thread r2 = new Thread(new MyRunnable2());
r1.start();
<span style="white-space:pre"> </span>//让线程睡眠5秒
Thread.sleep(5000);
r2.start();
}
}