473,797 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4808
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.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 <pe***@cplusplu s.org> wrote in message
news:3k******** *************@n ews20.bellgloba l.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.emai l> wrote in message
news:40******** @news.wineasy.s e...
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
14549
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 synchronized objects. However, why couldn't one simply declare: private synchronized LinkedList l; and have the variable be automatically synchronized, instead of having
4
8059
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 when to use one and not the other? For instance, if I wanted to remove an item in a SortedList, would it be better to lock() it or do it via the Synchronized wrapper? Why choose one over the other SortedList sl lock(sl sl.Remove(item) O
2
1698
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 method passed to ThreadStart. Try it, and please prove me wrong. ******** using System; using System.Threading;
0
1166
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 MyList : ArrayList { }
8
79893
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 void methodName() {...} What is the C# alternative for this?
4
8192
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 System.Collections.Generic.Queue? Putting it another way can I safely replace a System.Collections.Queue.Synchronized(myUnSynchronizedQueue) with a System.Collections.Generic.Queue while porting a working 2003 project? Thanks,
1
8384
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() { this.datset = new ArrayList();
7
7257
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
7730
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 need: (see the comments in source) public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { //..Here... or maybe somewhere elsewhere I need on my disposal the raw XML code of every XML tags...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9536
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10245
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.