Is static function thread safe ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Abhinay
    New Member
    • Jul 2006
    • 44

    Is static function thread safe ?

    Hi there,

    I am developing server in java which accept client request in multi threaded fashion and response back, each client request will be process by separate dedicated server thread.

    I have write static function to read data from client socket at once
    prototype:
    Code:
    public static ISOMessage getDetails( final DataInputStream clientInputStream );
    is this static function thread safe ???
    I am not using clientInputStre am more else where in flow ( in current thread )

    your help makes me happy,

    Thank you
    Abhinay
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You didn't show us the code for that static method; there are no special reasons why or why not a static method would (not) be thread-safe; i.e. the same rules count: synchronize on shared resources and your method will be thread-safe.

    kind regards,

    Jos

    Comment

    • Abhinay
      New Member
      • Jul 2006
      • 44

      #3
      hi josah,
      I appreciate your quick response,
      here is defination of my static function
      Code:
      public static Message getDetails( final DataInputStream clientInputStream )throws Exception
      {
      
       //Read message length
       byte bMsgLen[] = new byte[2];
       clientInputStream.readFully( bMsgLen );
      
       //Convert byte to integer
       int iMsgLen = byteToInteger( bMsgLen );
       //Read message 
       byte bMessage[] = new byte[ iMsgLen  ];
       clientInputStream.readFully( bMessage );
       
       .............. some business logic not related with client socket  ..........
      
       return new Message( bMessage  );
       
      }
      this function is used by all of my server thread to extract data from incoming client socket as it is multithreaded every time there will be different socket passed to specified function.

      Is my static function thread safe ??
      Should I used synchronisation while dealing with socket ??

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        You will need to synchronize access to any shared resources then.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Abhinay
          Should I used synchronisation while dealing with socket ??
          If no other thread uses that Socket there is no reason to synchronize on it.

          kind regards,

          Jos

          Comment

          • Abhinay
            New Member
            • Jul 2006
            • 44

            #6
            Hello,

            thanks again for taking interest in question.

            As per my knowledge when server (server socket) accept client request, it assign new dedicated socket for further communication with client and go back to original server socket and watch for new client request.

            In my server each client request is handaled by dedicated server thread so I guess received socket ( client request ) will not share by two thread.

            please suggest me.

            Thank you
            Abhinay

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Abhinay
              please suggest me.
              All has been suggested already: if a resource, any resource, a simple variable, a socket, a file, whatever, is used by two or more threads you have to synchronize those threads on that resource. If a resource is used by one thread only, you don't have to implement any precautions.

              kind regards,

              Jos

              Comment

              • Abhinay
                New Member
                • Jul 2006
                • 44

                #8
                thank you Jos !!!!!!!!!

                Comment

                Working...