By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Will this code need to be synchronized ?

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Thu, 27 May 2004 16:16:07 +0800, jackie wrote:
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.


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
Jul 17 '05 #2

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

- perry

jackie wrote:
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.


Jul 17 '05 #3

P: n/a
Corrected verion :

1 public static String toGBString( String asciiStr )
2 {
3 String s = "";
4 try {
5 s = new String( asciiStr.getBytes(), "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 <pe***@cplusplus.org> wrote in message
news:3k*********************@news20.bellglobal.com ...
given that java String objects are immutable, that is, they do not
change you should not have to synchronize this....

- perry

jackie wrote:
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.

Jul 17 '05 #4

P: n/a
On Fri, 28 May 2004 11:17:20 +0800, jackie wrote:
Do my concept correct ?


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
Jul 17 '05 #5

P: n/a
Thanks !!

Gordon Beaton <no*@for.email> wrote in message
news:40********@news.wineasy.se...
On Fri, 28 May 2004 11:17:20 +0800, jackie wrote:
Do my concept correct ?


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

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.