代码分析Java中线程的等待与唤醒
我们先来看一下实例代码:
classThreadAextendsThread{
publicThreadA(Stringname){
super(name);
}
publicvoidrun(){
synchronized(this){
System.out.println(Thread.currentThread().getName()+"callnotify()");
notify();
}
}
}
publicclassWaitTest{
publicstaticvoidmain(String[]args){
ThreadAt1=newThreadA("t1");
synchronized(t1){
try{
//启动“线程t1”
System.out.println(Thread.currentThread().getName()+"startt1");
t1.start();
//主线程等待t1通过notify()唤醒。
System.out.println(Thread.currentThread().getName()+"wait()");
t1.wait();
System.out.println(Thread.currentThread().getName()+"continue");
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}
输出结果:mainstartt1->mainwait()->t1callnotify()->maincontinue
其实调用t1.start(),t1为就绪状态,只是main方法中,t1被main线程锁住了,t1.wait()的时候,让当前线程等待,其实是让main线程等待了,然后释放了t1锁,t1线程执行,打印t1callnotify(),然后唤醒main线程,最后结束;
这里说一下wait()与sleep()的区别,他们的共同点都是让线程休眠,但是wait()会释放对象同步锁,而sleep()不会;下面的代码t1结束之后才会运行t2;能够证实这一点;
publicclassSleepLockTest{
privatestaticObjectobj=newObject();
publicstaticvoidmain(String[]args){
ThreadAt1=newThreadA("t1");
ThreadAt2=newThreadA("t2");
t1.start();
t2.start();
}
staticclassThreadAextendsThread{
publicThreadA(Stringname){
super(name);
}
publicvoidrun(){
synchronized(obj){
try{
for(inti=0;i<10;i++){
System.out.printf("%s:%d\n",this.getName(),i);
//i能被4整除时,休眠100毫秒
if(i%4==0)
Thread.sleep(100);
}
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}
}热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下