1 理解线程、创建线程
1.1 线程:程序中某一条执行线索
1.2 创建线程的方式
继承Thread和实现Runnale接口
**
* Description:两种创建线程的方法,extends Thread和 implements Runnable
*/
// 1. 通过extends继承Thread
class CreateThread1 extends Thread {
@Override
public void run() {
while(true) {
System.out.println("This is " +
Thread.currentThread().getName());
}
}
}
// 2. 通过implements实现runnable接口,创建资源对象
class CreateThread2 implements Runnable {
@Override
public void run() {
while(true) {
System.out.println("This is " +
Thread.currentThread().getName());
}
}
}
public class TestCreateThread {
public static void main(String[] args) {
//以第一种方式创建线程并启动 (extends Thread)
CreateThread1 thread1 = new CreateThread1();
thread1.start();
//以第二种方式创建线程并启动 (implements Runnale)
CreateThread2 t = new CreateThread2();
Thread thread2 = new Thread(t);
thread2.start();
while(true) {
System.out.println("This is " +
Thread.currentThread().getName());
}
}
}