8 Replies Latest reply: Feb 21, 2008 1:56 AM by 791266 RSS

    no need synchronization ?

    807603
      Why the static methods of class Math are not synchronized?

      because these static methods contains one or two statements. true?
        • 1. Re: no need synchronization ?
          807603
          yayathi wrote:
          Why the static methods of class Math are not synchronized?
          More important, give me a reason why they should be synchronized???
          • 2. Re: no need synchronization ?
            807603
            to clear my concepts...
                 My question is:

                 If the method returnData() call from different classes at same time, what will happen

                      (1).Both thread executing the different copy of this method?
                 If (1) is ok then no need to look at (2)
                 (2).
            Java_Deity wrote:
            More important, give me a reason why they should be synchronized???
            If both thread executing same copy then :returnData(1,2), returnData(5,6)suppose the first thread going to execute the last statement(same time the second thread executed the first instruction) then Is there possible to return 11 by first thread ?

            -----------------------------
            public static String returnData(int a, int b){
                      a = a+b;  
                      //here some another code
                      int c = a * b;
                      //here some another code
                      return String.valueOf(a); 
            }
            What is actually happening (1) or (2) or anything else??

            Edited by: yayathi on Feb 20, 2008 10:29 PM
            • 3. Re: no need synchronization ?
              807603
              is that my question absolute wrong..?
              • 4. Re: no need synchronization ?
                807603
                yayathi wrote:
                is that my question absolute wrong..?
                It doesn't make any sense to me. I still don't see any reason why you'd worry about thread safety here.
                • 5. Re: no need synchronization ?
                  807603
                  that means case (2) will not be happen. right?
                  • 6. Re: no need synchronization ?
                    791266
                    What is actually happening (1) or (2) or anything else??
                    There aren't multiple copies of the method, but each invocation of the method places the arguments on the stack that is local to the thread, so two different threads that are execution the same method will not share the arguments.

                    Kaj
                    • 7. Re: no need synchronization ?
                      807603
                      thanks..
                      so we can make synchronized if that method use global variables. ok?
                      • 8. Re: no need synchronization ?
                        791266
                        yayathi wrote:
                        thanks..
                        so we can make synchronized if that method use global variables. ok?
                        global variables? I guess you mean attributes? Yes, you should in that case make it synchronized.