2

I have a question concerning the java synchronization with static methods.

More precisely, I have a class with static methods that can be used concurrently by several threads. The principal static method of my class has one argument and calls the other auxiliary static methods one after the other passing them this argument.

My question is the following: since the class can be used by multiple threads at a time, isn't there a risk that another thread replaces the argument by another one?

I had thought of using a synchronization block on the argument variable of my principal function encompassing the whole code of the method (and thus the calls to auxiliary functions too), but I'm not sure it is a good solution.

Could anyone help me?

bfnv1807
  • 21
  • 1
  • 1
    How can a separate thread jump into another thread's stack frame and replace an argument value? – Sotirios Delimanolis Mar 25 '14 at 20:32
  • No, the arguments cannot be modified by being called by threads since Java is pass by value. – Luiggi Mendoza Mar 25 '14 at 20:34
  • What is the argument's type? A collection or array? – deanosaur Mar 25 '14 at 20:38
  • Even if the argument is a reference to the class (this) that called the static method? – bfnv1807 Mar 25 '14 at 20:38
  • what does it do to the class? do the method alter the state in any way? that is, do they update static values in the class? – deanosaur Mar 25 '14 at 20:45
  • The argument passed to my static methods (in static class, say A) is a reference to the calling class instance (say B) and the static methods of A use this reference to get information from B and perform operations in B. – bfnv1807 Mar 25 '14 at 20:53
  • If each thread is executed from a separate B object, and if the B objects do not share data among themselves, then you're fine. If your static methods in A are calling methods on B objects that do share data, then you'll need to make those data objects threadsafe. – deanosaur Mar 25 '14 at 21:00
  • No, the B objects do not share any data. Threads only share static data in A. – bfnv1807 Mar 25 '14 at 21:08

2 Answers2

2

My question is the following: since the class can be used by multiple threads at a time, isn't there a risk that another thread replaces the argument by another one?

No there isn't. You are confusing static and stack storage.

 static int x;

 static void someMethod(int y1, SomeObject y2) {
     int z;
     ...
 }

In a threaded situation, all threads access the same field x. You need to worry about synchronization of that field. However, even though someMethod(...) is a static method, the y1 argument and the z method field are local to the calling threads. Other threads can't access that memory since its on the thread's call stack.

The exception to this is the argument y2 since it is an object. Java is pass by value and primitives are passed on the stack. However when you pass an object by value, you pass its reference so two threads could get passed the same object reference and you would need to worry about synchronization there.

As an aside, calling static methods between threads although certainly allowed is not the best pattern. Instances are the way to go if possible. They can share static constant fields and the like but calling various static methods from other static methods sounds overly confusing.

Gray
  • 115,027
  • 24
  • 293
  • 354
0

I don't think it is possible for the value of the argument to be "replaced" by another thread... threads have their own method context... the so-called call-stack. For example, you could have 50 threads calling String.valueOf() at the same exact time for different values, and each would get the value that is within their own thread context.

TTT
  • 1,952
  • 18
  • 33
  • You need to be aware of the argument type. If the argument is non-thread save collection, then its values may change unexpectedly. – deanosaur Mar 25 '14 at 20:37
  • @deanosaur Static method doesn't imply there is only one execution context. – Dirk Lachowski Mar 25 '14 at 20:40
  • 1
    The OP is asking if a parameter that one thread passes to a method INDEPENDENT of another thread can be changed by that other thread merely calling that method with its own parameter, and the answer to that would be no. If we were talking about worker threads that crunch data on a collection, where you need to sync the threads in order to process that collection on your main thread, then what you said would be true deanosaur, with bad thread sync your collection can be modified by another thread while your main thread believes it is OK to proceed. – TTT Mar 25 '14 at 20:41
  • @rpg711 actually I think he's passing the same argument (the class itself) from all threads. What would happen if the class had a static member `boolean busy`, the first thread set it to true, and the static method did `while(busy)`? – deanosaur Mar 25 '14 at 20:49
  • That would depend on whether or not the other threads can access this. I think your example is bad code, static members do not even exist on the "this" instance reference, they exist on the class itself and are type-qualified, not instance qualified. Passing this is the exact same thing as passing a reference to any other mutable object you can think of, in which case the SAME RULES apply for what happens when you modify the values contained within that object from another thread. In this case you need to place a lock on the STATE of that object, which is not the issue here. – TTT Mar 25 '14 at 20:54