Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Problems with StringBuffers synchronized methods

Thread view: 
chris brat - 15 Jul 2006 06:39 GMT
Hi,

A colleague of mine recently told me that he once wrote a web
application that made very heavy use of StringBuffer class (JVM version
1.3) in order to generate code (using Javacc) and experienced the
problem of running out of file handles.

It was explained to me that the problem was due to the synchronized
methods of the StringBuffer class and all the thread locking associated
with using it.

This sounds very interesting and could explain a problem I had at a
previous company.

Thanks
Chris
Philipp Leitner - 15 Jul 2006 17:59 GMT
> Hi,
>
[quoted text clipped - 12 lines]
> Thanks
> Chris

Not that I could much informative here, but it also is an interesting
point concerning a problem that I currently have at work ... does
anybody know anything more on this?

/philipp
Mike Schilling - 16 Jul 2006 02:27 GMT
> Hi,
>
[quoted text clipped - 6 lines]
> methods of the StringBuffer class and all the thread locking associated
> with using it.

Unless the StringBuffer instances are used in multiple threads, the fact
that their methods are synchronized should be, at worst, a minor performance
issue.  If they are used in multiple threads (unlikely, for code
generation), they'd better be synchronized.

Nonetheless, in Java 1.5 and later, for single-threaded use StringBuilder is
preferred.
Chris Uppal - 16 Jul 2006 10:34 GMT
> It was explained to me that the problem was due to the synchronized
> methods of the StringBuffer class and all the thread locking associated
> with using it.

I'm having difficulty imagining any possible way that locks and files could be
connected.

Here are the closest I can get.

a)  StringBuffer operations are synchronised, hence slower, thus the String
processing took longer -- say it doubled the overall time needed to process one
requiest.  That means that the average number of requests "in flight" at any
one time will double (or more than double).  That in turn means that the number
of files open at any one time doubles (or more than doubles).  Which causes the
overall app to exceed some system limit.

b) For some unimaginable reason the app had several/many threads writing to
many StringBuffers as a many-to-many relationship (/very/ strange application
design!).  So each of the many SBs is /actually/ contended.  This means that
the JVM has to "inflate" the locks used for the objects from "thin locks" into
full OS-level semaphores.  If the OS was such that the "handles" allocated for
the semaphores came out of the same pool as were used for open files (but
why??), then a huge number of semaphores might exhaust the available file
handles.

c) Same as (b) but assuming that the 1.3 JVM didn't have the thin lock
optimisation, in which case the huge number of symultaneous SBs would might
force the JVM to allocate lots of OS-level semaphores even though the SBs were
not actually contended.  I can't remember when the thin lock optimisation was
added, I believe that the previous implementation used hashing scheme to reduce
the number of semaphores needed (several independent objects shared one
OS-level semaphore).

I suppose (c) is the least implausible, especially if you further assume that
"running out of file handle" is just a confused expression for "running out of
OS-level semaphores".

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.