3 Replies Latest reply: Apr 10, 2014 9:12 PM by Gamage RSS

    Two different servlet call one function

    Hjava

      I have one display function is that safe have servlet A and servlet B access the this same function?

        • 1. Re: Two different servlet call one function
          gimbal2

          That depends on what this display method exactly does. Generally if that display method doesn't make any changes to any kind of data, it will be safe. Otherwise you might need to apply synchronization to make it safe.

           

          Imagine that you ask "I leave the door of my house open to let fresh air in, is that safe to do?" Yeah MAYBE, depends on your neighborhood and if you have a dog or not My point being that it is not a yes/no answer.

          • 2. Re: Two different servlet call one function
            Hjava

            I only have select on the java class, two servlet will call this class.according your  answer, it should be safe.

            • 3. Re: Two different servlet call one function
              Gamage

              if your function code is not synchronized . that clearly can access servlet A and B. above gimbal2 said as if your home main door open so any one can come and go.

               

              example -

                             public List synchronized doSomething(String s) {

                                  List list = new ArrayList();

                                 // some thing

                                  return (list);

                             }

               

              if this method called by doGet() or DoPost()  of servlet A. this moment key have in Servlet A so servlet B must need to waiting yet leave key by Servlet A.

              if you have use synchronized for your model code that is thread safe. two servlet can't be access same time.

               

              finally , if you need a thread safe you must need to use synchronized for your model class method.