ilovetolearn
ilovetolearn

Reputation: 2060

How do I analyze Java source code and ensure it is Thread safe

I am reading through java codes to ensure it is thread safe.

As I understand, any local variables within the method is thread safe since it belongs to the stack memory address. Any class / instance variables is not thread safe as it belongs to the heap memory is shared by other thread.

By rule of thumb, I can put a synchronized keyword on every method which touches the class variables.

Is there any eclipse plugin, or rules I can analyze / prevent multi-threading issues?

Upvotes: 5

Views: 4650

Answers (6)

dsannella
dsannella

Reputation: 79

ThreadSafe (http://www.contemplateltd.com/threadsafe, free trials available) does exactly what the question asks for. See this InfoQ article for examples of concurrency errors it finds in open source applications including Apache JMeter and K9Mail. See Ivan Senic's blog for a user's opinion. ThreadSafe's Wikipedia entry gives a brief summary.

(Disclosure: ThreadSafe is a commercial tool, and I'm co-founder of Contemplate, the company that produces it.)

Upvotes: 0

assylias
assylias

Reputation: 328618

More of a comment but a little too long.

By rule of thumb, I can put a synchronized keyword on every method which touches the class variables.

Not really - Think for example of the Vector class: it is synchronized however iteration requires external locking. A concurrent alernative is CopyOnWriteArrayList, for example, which allows iteration without needing to lock the whole collection.

Bottom line as already answered by others: it is not as simple as adding synchronized everywhere. You need to precisely analyse the contract of the class and make sure that the contract is still fulfilled in a multi threading situation.

Upvotes: 0

Alex Edwards
Alex Edwards

Reputation: 1673

I don't think there is anything that will definitively check for thread safety, there are some tools that have already been mentioned, like findbugs that will do a reasonable job of finding the obvious mistakes.

It is very much up to the programmer to ensure that their program is not leaking variables or references into different threads and where things are used in multiple threads ensuring that each thread see's the 'correct' value.

Design for safety before performance, you might find that it performs fine for your needs but if you put optimisation in you increase complexity and potentially failure, it might not end up being the bottleneck.

I would recommend reading reading specifically Java Concurrency In Practice, you may also find Effective Java helpful as well.

Upvotes: 2

Mik378
Mik378

Reputation: 22171

Threading world is one of the most touchy thing for a good programmer. Every solution to thread issues requires a big knowledge of the context. Any plugins would not be enough "intelligent" to choose the most relevant solution everytime.

Indeed, synchronized represents about 5% of every possible solutions. Think to concurrent collections, ConcurrentHashMap for example which is very well thought and doesn't use a basic big locking on it => more more studied complex than that.

Think to volatile, to perform ensure barrier memory while avoiding any kind of atomocity through any locks; atomicity is not its role, but in some cases, this would be great to avoid it to improve performance.

Therefore, forget any plugins (even if some may exist) for thread-safety but trust your brain ;)

Note: Putting synchronized keyword on each writing class, would be.........ugly without talking about the poor performance...

Upvotes: 5

Deepak Bala
Deepak Bala

Reputation: 11185

I've not seen compile time tools that can detect deadlocks. That would be difficult to model. All I know is that some people have tried.

You can always detect them at runtime. JMX Beans can be queried to show the deadlocked threads as explained in this answer - Deadlock detection in Java.

Upvotes: 0

Lokesh
Lokesh

Reputation: 7940

I dont think eclipse will provide any such plugin. Along with local variables try to use Immutable objects ,ThreadLocals and most od multithreading issues will be taken care of.

Upvotes: 0

Related Questions