Are wait() and notify() unreliable despite of synchronized?
问题 I recently discovered that using synchronized won't prevent any dead locks. E.g. within this code: ArrayList <Job> task; ... public void do(Job job){ synchronized(tasks){ tasks.add(job); } synchronized(this){ notify(); } } public void run(){ while(true){ for (int = 0;i<tasks.size();i++){ synchronized(tasks){ Job job = tasks.get(i); } //do some job here... } synchronized(this){ wait(); //lock will be lost... notifier = false; //lock will be acquired again after notify() } } } Now, what is the