views:

82

answers:

6

There is a rule to find out, for sure, all objects that could possibly have concurrent access in a Java program? My intention is use such rule, if it exists, to find out which Java classes could possibly have concurrent access and then guarantee that they are thread-safe. This rule could be very useful when inspecting a large Java project. I've thought to look for static methods, but I’m not sure if this is the only way an object can be accessible to multiple threads.

+1  A: 

No, it's not possible to determine this from looking at the method signatures. There is no rule that only static methods can give concurrency problems.

There could also be a problem if two threads share references to the same objects, either directly (by being given a reference to the same object) or indirectly (via static objects, static methods, members of objects used, etc.). It is not always possible to detect these situations using a static analysis of the code.

Mark Byers
+2  A: 

It seems very, very unlikely that such a thing exists as a single rule. Imagine the following:

public void maybeAccessConcurrently(Program x) {
    if(halts(x)) {
        accessConcurrentObject();
    } else {
        // don't.
    }
}

I don't know about you, but I can't really contrive a rule that isn't equivalent to deciding the halting problem here, especially not by simple inspection of types or declarations. I think you may find that like other static analysis techniques, there are a set of properties that you can check within reason. The Bandera project appears to be an effort to use various model-checking techniques to verify properties of concurrent Java code. There may be other similar efforts (all of which are most likely to involve model checking, given the presence of concurrency).

Gian
A set of rules that help to detect classes that *may* suffer concurrent access would help too.
Renan Vinícius Mozone
At the risk of collecting many, many false positives (and some false negatives), you would in that case basically need to build a dependency graph and then try to isolate disjoint portions of code that were within different threads. I think using some concurrent model-checking software such as that which I linked would probably be much, much easier.
Gian
+2  A: 

I've thought to look for static methods, but I’m not sure if this is the only way an object can be accessible to multiple threads.

No, static methods don't help you at all. You're not even restricted to objects. Even access to primitives may not be thread safe. For example incrementing a long may not be an atomic operation depending on the platform you're running on. Afaik what you're asking is not possible.

Raoul Duke
+1  A: 

I doubt that such a (handy) rule is possible. Consider this:

public MyRunnable implements Runnable {
  public run() {
    Class.forName("com.example.MainClass").newInstance();
  }
}

and later

public class MainClass {
   public MainClass() {
     startApplication();
   }

   public static startApplication() {
     // A huge application is started
   }
}

Now every class that is ever used while executing the "MainClass" application has concurrent access - because you can start the application several times in different threads and you'll never know which class is started (the information is hidden in a String literal) and what other classes may be instantiated.

Andreas_D
+5  A: 

Yes it is very possible to make such a rule,

public static boolean canBeAccessedConcurrently(Object arg){
        return true;
}
Andrew
Lol true, true. Though you might want to accept an Object to match up with the OP's question.
Mark Peters
I like this. Reminds me of my favorite random number generator: http://xkcd.com/221/ :-)
seanizer
+1 for hilarity :)
Raoul Duke
But also this makes a really good point. The OP would be better off looking for classes that *can't* be accessed concurrently. It would be a much smaller set, possibly empty.
EJP
+2  A: 

Check out static code analyzers FindBugs and PMD. They have a lot of rules that help to find potential concurrency problems. Both can be run from the command line, so you can incorporate them into your build and have the build broken if rules are violated.

Slava Imeshev