• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is method parameters passed to static methods thread safe??

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have a doubt regarding method parameters passed to static methods in java. Suppose I have a static method on a object A, say
public static String void process( String b, String c, SomeObject X)
{
}
1.) The Static Method modifies SomeObject X.
2.) The Static Method modifies b and c.
3.) The static Method returns a String result.
Is this Thread Safe, if multiple clients access this method supplying
their params(b, c and X). I would really appreciate it if some one could clarify this for me.
Sudharsan.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. If the method modifies the instance that X points to, and some other thread calls this method with the same X instance, threading issues could arise.

Since you didn't say that the method modifies any static variables within A, you should be okay so long as you call process() with different SomeObject instances.

2. As String is immutable, you cannot modify the Strings b and c point to. You *can* point those local variables to different Strings, but again this causes no trouble.

3. Remove the "void" from the declaration, then, for as it's written it will not compile.
 
sudharsan, varadharajan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello David,
Thanks a lot for the quick reply.
1.) Regarding your reply point no 1, in my case, SomeObject X is a java.util.Date object instantiated from the caller object. Each caller object instantiates its own date object and so I am safe.
2.) Regarding your reply point no 2, Yes, i did mean, concatenation of 2 strings to point to a new resultant string and not truly "modification" of the original strings b and c.
3.) Regarding your reply to point no 3, I didn't want to write a complete compilable method within my message. i just wanted to get my question across clearly. Yes. It needs a "return String " at the end to compile.
Again, thanks a lot.
Sudharsan.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because strings are immutable, this will always be thread safe. Java passes references into methods by value. Each method invocation gets its own copy of the reference. The variables b,c, X are private to the particular method invocation.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not that sure.


when A.foobar is invoked, another method might change a, and change b later.
Whether Strings are immutable, and methods are static, doesn't have any influence on the question of threadsafety.

When multiple threads share a, b, and X, there is no threadsafety, and if not, you don't need a static method or immutable objects.
[ September 16, 2004: Message edited by: Stefan Wagner ]
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point, Stefan!

(As an aside, I keep my fingers crossed for you, but in my experience Berlin isn't a very good place to find an IT job... )
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic