Thread和Runnable 的使用
Thread:
public class TestThread {
?public static void main(String[] args) {
??MyThread thread1 = new MyThread();
??MyThread thread2 = new MyThread();
??thread1.start();
??thread2.start();
?}
}
public class MyThread extends Thread{
?@Override
?public void run() {
??System.out.println(this.getName()+"线程被执行");
??try {
???Thread.sleep(1000);
??} catch (InterruptedException e) {
??}
??System.out.println(this.getName()+"线程关闭了");
?}
?
}
Runnable接口:
public class TestThread2 implements Runnable{
?public void run() {
??System.out.println("线程启动");
??try {
???Thread.sleep(2000);
??} catch (InterruptedException e) {
??}
??System.out.println("线程结束");
?}
?
?public static void main(String[] args){
??TestThread2 t = new TestThread2();
??Thread t1 = new Thread(t);
??Thread t2 = new Thread(t);
??t1.start();
??t2.start();
?}
}