线程两种方式:
1.extends Thread
package study01;
public class ThreadTestExtends extends Thread{
@Override
public void run() {
for(int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2.implements Runnable
package study01;
//is a
public class ThreadTest implements Runnable{
@Override
public void run() {
for(int i = 0; i < 10; i++) {
System.out.println(i + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
3.启动线程的方式
package study01;
public class Yunxing {
public static void main(String[] args) {
//来自继承的线程启动方式
System.out.println(Thread.currentThread().getName());
ThreadTestExtends t1 = new ThreadTestExtends();
t1.start();
ThreadTestExtends t2 = new ThreadTestExtends();
t2.start();
//来自实现接口的线程的启动方式
Thread t3 = new Thread(new ThreadTest());
t3.start();
}
}