一、创建一个线程有两种方法:
推荐1.将一个实现了Runnable接口的类作为Thread类的构造参数
public class A implements Runnable {
public void run(){
while(true)
System.out.println("Runnable");
}
}
public static void main(String[] args) {
A a = new A();
Thread t = new Thread(a);
t.start();
// MyThread myThread = new MyThread();
// myThread.start();
while(true)
System.out.println("main");
}
2.通过继承Thread的子类实现
public class MyThread extends Thread{
public void run(){
while(true)
System.out.println("myThread");
}
}
二、
1.sleep() 哪个线程执行sleep,哪个线程就睡