Java Sync Help

looking at http://download.eclipse.org/jetty/stable-7/xref/com/acme/ChatServlet.html , I don't seem to understand why there should be a synchronization block in a synchronized method, like this:

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;
                }
            }
        }
    }

Why do I mneed to synchronize (again?) If the whole method is already thread safe?

Thank you for your understanding.

+3
source share
3 answers

"chat (...)" , (m) "m", . , - .

+4

, this. , .

+1

.

synchronized , , this, .

synchronized(m) , , m, .

0
source

Source: https://habr.com/ru/post/1731743/


All Articles