Will this code need to be synchronized ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jackie

    Will this code need to be synchronized ?

    public static String toGBString( String asciiStr )
    {
    String s = "";
    try {
    rtnStr = new String( ascii.getBytes( ), "gb2312" );
    } catch( Exception ex ) {}
    return s;
    }


    I am not sure if this kind of routine require to be synchronized.


  • Gordon Beaton

    #2
    Re: Will this code need to be synchronized ?

    On Thu, 27 May 2004 16:16:07 +0800, jackie wrote:[color=blue]
    > public static String toGBString( String asciiStr )
    > {
    > String s = "";
    > try {
    > rtnStr = new String( ascii.getBytes( ), "gb2312" );
    > } catch( Exception ex ) {}
    > return s;
    > }
    >
    >
    > I am not sure if this kind of routine require to be synchronized.[/color]

    Your method is nonsensical for several reasons:

    - there does not appear to be any variable called "ascii" in scope,
    and you have ignored the "asciiStr" argument passed to the method.

    - you throw away rtnStr and always return "".

    - Strings in Java use unicode. There is no "character encoding"
    involved, so the conversion you are attempting is not meaningful.
    Character encoding is part of the conversion to and from byte
    arrays. It says how the byte array should be interpreted, and has
    nothing to do with the Strings themselves.

    - ascii.getBytes( ) does not specify what character encoding should be
    used for the conversion to byte[], so the resulting bytes you get
    might not even be ASCII, depending on the default character encoding
    on your platform. Always specify a character encoding when
    converting from String to byte[], or from byte[] to String.

    - if you have a byte[] containing the ASCII representation of some
    characters, then you should specify ASCII when converting it to a
    String. If you take a byte[] containing some given representation of
    a String and convert it to a String using a different character
    encoding, the results will not be meaningful.

    Finally, since your method does not modify any variables external to
    the method itself, there is no synchronization necessary. However this
    is hardly your first concern here.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e

    Comment

    • perry anderson

      #3
      Re: Will this code need to be synchronized ?

      given that java String objects are immutable, that is, they do not
      change you should not have to synchronize this....

      - perry

      jackie wrote:[color=blue]
      > public static String toGBString( String asciiStr )
      > {
      > String s = "";
      > try {
      > rtnStr = new String( ascii.getBytes( ), "gb2312" );
      > } catch( Exception ex ) {}
      > return s;
      > }
      >
      >
      > I am not sure if this kind of routine require to be synchronized.
      >
      >[/color]

      Comment

      • jackie

        #4
        Re: Will this code need to be synchronized ?

        Corrected verion :

        1 public static String toGBString( String asciiStr )
        2 {
        3 String s = "";
        4 try {
        5 s = new String( asciiStr.getByt es(), "gb2312" );
        6 } catch( Exception ex ) {}
        7 return s;
        8 }

        I am curious what will happen if Object A calls this rountine and e.g. s =
        \u1234 at line 5
        At the same time the thread paused by the System(may be the system too busy)
        and another Object B calls this rountine, change s = \u5678 at line 5
        So, at this time s = \u5678, but since Object A does not finish its' call
        and now the program continues by the System.
        Finally, both of them return s, ie. \u5678

        Do my concept correct ?



        perry anderson <perry@cplusplu s.org> wrote in message
        news:3kntc.3362 7$sr3.1033380@n ews20.bellgloba l.com...[color=blue]
        > given that java String objects are immutable, that is, they do not
        > change you should not have to synchronize this....
        >
        > - perry
        >
        > jackie wrote:[color=green]
        > > public static String toGBString( String asciiStr )
        > > {
        > > String s = "";
        > > try {
        > > rtnStr = new String( ascii.getBytes( ), "gb2312" );
        > > } catch( Exception ex ) {}
        > > return s;
        > > }
        > >
        > >
        > > I am not sure if this kind of routine require to be synchronized.
        > >
        > >[/color]
        >[/color]


        Comment

        • Gordon Beaton

          #5
          Re: Will this code need to be synchronized ?

          On Fri, 28 May 2004 11:17:20 +0800, jackie wrote:[color=blue]
          > Do my concept correct ?[/color]

          No.

          s is local to the method, and is therefore *unique* for every caller.
          Several threads can call this method and make whatever changes they
          like to s. They cannot possibly affect one another, regardless of
          timing issues.

          /gordon

          --
          [ do not email me copies of your followups ]
          g o r d o n + n e w s @ b a l d e r 1 3 . s e

          Comment

          • jackie

            #6
            Re: Will this code need to be synchronized ?

            Thanks !!

            Gordon Beaton <not@for.emai l> wrote in message
            news:40b6bb80$1 @news.wineasy.s e...[color=blue]
            > On Fri, 28 May 2004 11:17:20 +0800, jackie wrote:[color=green]
            > > Do my concept correct ?[/color]
            >
            > No.
            >
            > s is local to the method, and is therefore *unique* for every caller.
            > Several threads can call this method and make whatever changes they
            > like to s. They cannot possibly affect one another, regardless of
            > timing issues.
            >
            > /gordon
            >
            > --
            > [ do not email me copies of your followups ]
            > g o r d o n + n e w s @ b a l d e r 1 3 . s e[/color]


            Comment

            Working...