0

I have:

public class UTIL{
  public static void met(){
    do_something(){
       print(A.m()); 
    }
  }
}
public class A{
  public  <type> m;
  public <type>static m(){
    return m;
  }
}

Now:

Thread A contains instance of class A

Thread B contains instance of class A

From Thread B, at some point UTIL.met is called.

Question: When UTIL.met is called, will it use m from the instance of A in Thread B?

3 Answers 3

3

When UTIL.met is called, will it use m from the instance of A in Thread B?

No, it doesn't matter if Thread A has one instance and Thread B has another. A.m is static and common for all instances.

But it is definitely the case that if the variables are static, then both threads will use the same variable.

(In other words, without proper synchronization, you'll have nasty race-conditions.)

Sign up to request clarification or add additional context in comments.

5 Comments

how about if the variable isn't static?
Then you can't write A.m to begin with. (Of course aInstance1.m is different from aInstance2.m though.)
it was static at the beginning. How about if I have a static method returning a non static variable defined in each instance. Can I then use something like A.m()- which would be returning a non static m in each instance-?
If you have a static method that returns a new instance each time it is called (like a factory method), then everything should be completely thread safe.
alright, at least I got my answer, although I wished it would be simpler.
1

m is a class variable, so is is actually common for all instances.

UTIL.met() will use the same instance of m that is "being held" [or can be accessed is a better terminology...] in all instances of A.

2 Comments

yea, stupid me, how about if the variable is not static, because that is the case. I didn't formulate the question well enough
@Ameoo: If the variable is non-static it depends on how you assign it, each instance of A will have its own m - but they might be shared among some instances. [Consider m is being assigned in the constructor as for example m = System.out; - then all instances of A will actually share the same object in m.]
1

m is defined as a static variable in A. So it will common/shared among all instances of A. If m is not static on the other hand, then of course each instance will have its own copy.

Well since thread A and thread B each contains own instance of class A, then each thread will use its instance of A.

is that what you want to achieve or you wanted to share m between instances??

2 Comments

I actually used m as static so all classes in a thread would be able to easily use that. But when I wanted to do threads, it hit me that I need to figure something else, since, as i see from all the answers, static variables are shared between threads also.
if multiple threads are going to write on the same variable then you need to use locks or something similar so that you don't get the "dirty read" effect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.