//创建线程,重写run()方法
//线程开启不一定立即执行,由CPU调度
public class TestThread01 extends Thread {
public void run(){
for (int i = 0; i < 20; i++) {
System.out.println("我在看代码--"+i);
}
}
public static void main(String[] args) {
//主线程
TestThread01 testThread01 = new TestThread01();
//调用start();方法开启线程
testThread01.start();
for (int i = 0; i < 200; i++) {
System.out.println("我在学习多线程--"+i);
}
}
}
Runnable接口
//实现runnable接口
public class TestThread02 implements Runnable {
public void run(){
for (int i = 0; i < 20; i++) {
System.out.println("我在看代码--"+i);
}
}
public static void main(String[] args) {
//主线程
TestThread02 testThread02 = new TestThread02();
Thread thread = new Thread(testThread02);
thread.start();
//调用start();方法开启线程
for (int i = 0; i < 200; i++) {
System.out.println("我在学习多线程--"+i);
}
}
}