destroy 破坏线程 getstate 返回该线程的状态
1.新生(new)---start()
2.就绪状态----cpu调度
3.运行状态----sleep,join,wait 进入阻塞状态
4.死亡 sleep 睡眠(毫秒,纳秒)
wait 等等 进入阻塞状态 需要唤醒 notify
join‘插队强制让cpu执行当前的线程,其他线程阻塞。
yiled 让步,暂停
如何实现线程同步:给线程上锁(synchronized)
上锁:方法上锁,对象上锁。
一个小的多线程运行程序
public class Test {
public static void main(String[] args) {
Thread t=new Thread(){
public void run() {
pong();
}
};
t.run();
System.out.print("ping");
}
static void pong(){
System.out.print("pong");
}
}