初学线程,对于一些问题不是很明白,请看下面的示例:
public class WaitAndNotifyAndSynchronized
{
public WaitAndNotifyAndSynchronized(){
final WaitAndNotifyAndSynchronized a = this;
Thread th1 = new Thread(){
@Override
public void run(){
try
{
this.sleep(3000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
synchronized (a){ //①
System.out.println(this);//②
a.notify();
}
}
}
};
th1.setDaemon(true);//这句也是必须的,将th1设为守护线程,保证在唤醒a以后,所有活动的线程都为守护线程,jvm能及时退出
th1.start();//③ 和a.run的顺序不可以换
this.run();
}
public static void main(String[] args)
{
new WaitAndNotifyAndSynchronized();
}
public void run()
{
/**
* 这里可以换成这样,直接锁住this就行了
*/
synchronized (this)
{
try
{
this.wait();//阻塞当前的线程并释放锁
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
System.out.println("1");//执行finally
}
}
}
}
1.关于①处,为什么notify和wait方法必须要同步,难道就是因为api里强制的规定吗?
2关于②处,最后的执行结果是不停的输出这里然后虚拟机退出,我的理解是notify之后此时就剩守护线程了,应该结束了,怎么还会不停的输出这里
3关于③处,为什么和a.run()不能换位置,对换执行的结果是什么都不输出,虚拟机不退出