English 中文(简体)
Thread Safety framework
原标题:

The following class is not thread-safe (as proven in Proving the following code not thread safe )

Is there a framework out there that can help with either compile time / run time analysis and tell us that the following is not thread safe?

For compile time, ideally in Eclipse the wiggly underline comes up and tells us that the class is not thread safe?

For run time, will any the static code analysis catch the class as non-thread-safe?

public class LazyInitRace {
   private ExpensiveObject instance = null;

    public ExpensiveObject getInstance() {
    if (instance == null)
      instance = new ExpensiveObject();
    return instance;
   }
}
最佳回答

FindBugs can find parts of your code where thread synchronization is inconsistent, i.e. you synchronize access to a field in one place but not in another. It can also do basic validation against JCIP annotations, but I believe only @Immutable is checked at the moment.

I don t know of any static analysis tool that would automatically catch this particular case but I m sure one exists.

问题回答

This is a classic problem called the double checked locking problem.

The problem is that you have a race condition because your check on whether instance is null and assigning the value. One solution to this problem that I like in Java is:

public class LazyInitRace {
  private static class Container {
    public final static ExpensiveObject INSTANCE = new ExpensiveObject();
  }

  public ExpensiveObject getInstance() {
    return Container.INSTANCE;
  }
}

The way this works is that the inner class isn t initialized until it s referenced (which gets you your lazy loading) and loading the class is an atomic and threadsafe operation.

There are however other valid solutions.

Although it has been long time since this question was asked or answered , today I came across this question while searching on google.

Is there a framework out there that can help with either compile time / run time analysis and tell us that the following is not thread safe?

www.contemplateltd.com , they have developed an advanced static analysis tool. But its not free.

For compile time, ideally in Eclipse the wiggly underline comes up and tells us that the class is not thread safe?

http://www.checkthread.org/index.html , This is opensource project you may want to see examples here





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签