查看http://download.eclipse.org/jetty/stable-7/xref/com/acme/ChatServlet.html,我似乎不明白为什么在同步方法中需要一个同步块,就像这样:
private synchronized void chat(HttpServletRequest request,HttpServletResponse response,String username,String message)
throws IOException
{
Map<String,Member> room=_rooms.get(request.getPathInfo());
if (room!=null)
{
// Post chat to all members
for (Member m:room.values())
{
synchronized (m)
{
m._queue.add(username); // from
m._queue.add(message); // chat
// wakeup member if polling
if (m._continuation!=null)
{
m._continuation.resume();
m._continuation=null;
}
}
}
}
如果整个方法已经是线程安全的,为什么还m
需要同步(再次?)?
感谢您的任何见解。