473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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.
Jul 17 '05 #1
5 4793
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frank | last post by:
Hi, In the javadocs regarding many of the java.util classes, it states that the classes are not synchronized, and suggest using the Collections.synchronizedX(...) methods for getting...
4
by: Rich Sienkiewicz | last post by:
Some classes, like Queue and SortedList, have a Synchronized method which gives a thread safe wrapper object for these classes. But the lock() statement does the same thing. Is there any rules as to...
2
by: pokémon | last post by:
OK, if you run this code below (Console app) you will find that threads will not always behave in a synchronized fashion, even with a lock statement, --unless you put a Sleep somewhere in the...
0
by: Mike Grasso | last post by:
I've seen a few messages on this, but no responses. Here's what I found out PROBLEM: How do you use ArrayList.Synchronized to create thread-safe objects derived from ArrayList public class...
8
by: ASP.Net programmer | last post by:
Hi, I have a few methods in a class that I want to synchronize (make sure they can't be used at the same time by multiple threads). As a Java programmer I just do this: public synchronized...
4
by: nhmark64 | last post by:
Hi, Does System.Collections.Generic.Queue not have a Synchronized method because it is already in effect synchronized, or is the Synchronized functionality missing from...
1
by: KK | last post by:
Dear All I have a class whose methods are getting called from multiple threads in my application. For example class DataDistribution { private ArrayList datset; public DataDistribution() {...
7
by: Alan Wang | last post by:
Hi there, How can I create synchronized method in vb.net like synchronized method in Java? Thanks in advanced Alan
5
by: PatlaDJ | last post by:
Java SAX parser, please need a clue how to get the raw XML code of the currently parsing event... needed for logging, debugging purposes. Here's and example, letting me clarify exactly what i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.