tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 49686] New: Using an instance lock to protect static shared data in class SocketConnector
Date Mon, 02 Aug 2010 05:50:06 GMT
https://issues.apache.org/bugzilla/show_bug.cgi?id=49686

           Summary: Using an instance lock to protect static shared data
                    in class SocketConnector
           Product: Tomcat 7
           Version: 7.0.0
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Modules: tomcat-lite
        AssignedTo: dev@tomcat.apache.org
        ReportedBy: feng_yu_jia@hotmail.com


SocketConnector has the unsafe synchronization as follow. 

public class SocketConnector extends IOConnector {
...
 static int id = 0;

    public synchronized NioThread getSelector() {
        if (selector == null) {
            String name = "SelectorThread-" + id++;
            selector = new NioThread(name, true);
        }

        return selector;
    }
...
}

id is a static shared data, and it is not proper to use a instance lock to
protect it, especially when two instance of the class are created.

it would be more safer to write this instead:

public static synchronized NioThread getSelector() {
        if (selector == null) {
            String name = "SelectorThread-" + id++;
            selector = new NioThread(name, true);
        }

        return selector;
    }

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message