views:

102

answers:

7

Dear all,

I have a static method as following:

public static void writeArticle(TypeA typeA, TypeB typeB) {
  AWriter writer =  AFactory.getWriter("aWriter");
  Article article = writer.newArticle();

  /* PARAMETER WRITE START */

  article.set("title", typeA.getTitle());
  article.set("author", typeB.getName());
  article.set("age", typeB.getAge());
  // …
  /* more set statments here */
  writer.write(article);
}

Could this method cause a problem that the writer will write a value-mixed Article? That is, when 2 class (Class A and ClassB) instances calling this method, will Article get some of typeA values from ClassA and some from ClassB?

+5  A: 

No. Why do you think the arguments from two different calls would get mixed up? There's no reason to think they would.

If this is a multi-threaded program, you should ofcourse be careful with sharing objects between threads; if those objects have mutable state (member variables that can be changed) you should take care that two threads are not modifying the state at the same time.

Jesper
There is actually multi-threading involved. ClassB I mentioned is actually an application accessing the utility static method, and ClassB invloves multi-threading logic, it also calls the method. ClassA is another application running at the same platform which does nnot have multi-threading logic. I only have a basic knowledge of threading, so that's why I ask question here, and seek some guidance.
sarahTheButterFly
You should synchronize at the appropriate places. This is not a small subject that can be explained in two sentences... a good tutorial can be found here: http://download.oracle.com/javase/tutorial/essential/concurrency/
Jesper
+4  A: 

Whenever you say static, you do not talk about instance method calls. Always static methods should be called with their class name.

The static method is totally isolated at instance level. Even though it might write a value-mixed, it is what you want it to write isnt it?

Bragboy
A: 

No. It would be hell otherwise.

foret
Do you think this is a helpful answer?
seanizer
@seanizer : seems a Ok answer to me. Answers always does not have to be elaborative. Infact he conveys what has to be conveyed.
Bragboy
@Bragboy: so why did you write a more helpful, more elaborate answer then?
seanizer
@seanizer: Chill dude..
Bragboy
@Bragboy will try :-)
seanizer
A: 

This would only be the case if both calls accessed and changed common state, e.g. assuming the class in which this method is defined is called ArticleWriter, they would both read and change static fields of ArticleWriter.

seanizer
+1  A: 

That would be impossible for your code. typeA has method getTitle and typeB has method getName. Which fields are getting mixed up?

fastcodejava
A: 

It's unknowable from this code snippet. The first question to address is whether or not this method may be called from two different threads at the same time. If so the problem is most likely that two threads are modifying the same object at the same time (either article or writer). The easiest way to address that would be to synchronize writeArticle().

Yes, I kind of know the solution. I'm just trying to understand the whole picture. so which object is the two threads modifying. They just pass in the parameters, they did not share anything. The only place I suspect is the write. Or do you think the article will get mixed values?
sarahTheButterFly
Like I said, it's unknowable from this code snippet. My guess, based on the semantics, is the problem lies inAFactory.getWriter("aWriter");returning a reference to the same instance of AWriter, which means multiple threads will share that instance. If you're going to have multiple threads running the same code at the same time the safest thing to do is have them use separate instances of all the objects they need. If they *must* have access to the same object then you need to make sure access to that object is properly synchronized.
+3  A: 

No, method parameters and local variables are thread-safe as long as they do not refer to shared objects.

gpeche