关于java同步的小问题
lxm7 2006-05-31 06:53:01 我在网上看到如是说:
wait(),notiify(),notifyAll() 必须在同步方法中使用!
我想问这是为什么?
我调用notifyAll() 在一个没有synchronized关键字的方法种不行么?我的class大概如下:
public class AAA
{
private static mapDict = new Hashtable();
private boolean init()
{
boolean bRet = false;
bCanRead = false; //设置mapDict对象为不可读
mapDict = new Hashtable();
mapDict.put("key","value");
bCanRead = true; //设置mapDict对象为可读
Thread.currentThread().notifyAll();
bRet = true;
return bRet;
}
public getValue(String key)
{
while(!bCanRead)
{
try{Thread.currentThread().wait();}
catch(InterruptedException ex){}
}
return mapDict.get(key);
}
}