Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do I use the Java API in a Thread-Safe Manner?

Thread Tools

How do I use the Java API in a Thread-Safe Manner?

Raymond DeCampo
Posts: n/a
Thomas G. Marshall wrote:
> Raymond DeCampo coughed up:
>>Thomas G. Marshall wrote:
>>>Raymond DeCampo coughed up:
>>>>Thomas G. Marshall wrote:
>>>>>Raymond DeCampo coughed up:
>>>>>>In general, I would assume that static methods are thread safe. Any
>>>>>>static method that is not would be considered to be broken by
>>>>>>any reasonable software engineer.
>>>>>But that would be a dangerous assumption to make.
>>>>>A static method can easily be MT unsafe. Static methods usually
>>>>>manipulate one or more class variables, and those could be mucked
>>>>>up just as easily as [object] member variables can.
>>>>In my experience, most static methods do *not* manipulate
>>>>class-level variables. Most are self contained. YMMV.
>>>>If you have static methods that do manipulate class variables, you
>>>>should consider rewriting the class to put such things in an
>>>Maybe, but your logic seems a little "cart before the horse" to me. If
>>>you have a static method, chances are it is there just so you
>>>*can* access/mutate class variables. Otherwise, there is probably
>>>little reason to have it at all.

>>Quite the opposite. Instance methods exist to manipulate instance
>>variables and state. If a method does not do this, it ought to be
>>static. Static methods are for routines that do not belong to a class
>>instance. For example, every method in the Collections class, every
>>method in the Arrays class, the various String.valueOf() methods, the
>>parseXxx() methods in the extensions of java.lang.Number, most of the
>>methods in SwingUtilities, etc.

> Hmmmm.... that's an interesting perspective to raise in this particular
> discussion: that if an instance method does not manipulate instance
> variables and state then it ought to be static. Manipulate would of course
> mean /both/ the accessing and mutation of them.

You've changed the subject. Of course methods designed to work
polymorphically or as part of an interface implementation cannot be made
static. But that is not the point of the discussion, that was merely a
reaction an explanation of part of the reasoning behind one of my
assumptions that does not go to the heart of the current topic.

The point is that I maintain that most static methods do not manipulate
class variables. I gave a slew of examples above. I further maintain
that any static method that is not thread safe should be considered
broken. Therefore is it reasonable to assume thread safety when dealing
with static methods. (That does not mean that you should not be aware
that no programmer is perfect and that bugs will happen.)


XML is the programmer's duct tape.
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
platform specific API or C standard API George2 C Programming 13 11-13-2007 06:29 PM
.Net Profiler API in 64 bit windows -FunctionMapper callback API =?Utf-8?B?TGVv?= Windows 64bit 0 09-05-2007 06:10 PM
Profiling API or Membership API John123 ASP .Net 0 10-20-2006 03:18 PM
Calling the C API from Python and Python program from same C API -bidirectional Praveen, Tayal (IE10) Python 0 03-17-2005 06:33 AM
What API replaces the unlock API that existed in gcc 2.9.3? Shlomo Anglister C++ 1 08-02-2004 06:50 PM