Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have been researching into when data is being accessed or shared by multiple threads within Java. And looking into the problems such as:

  • Thread Interference
  • Memory Consistency
  • Deadlock
  • Starvation

From this, solutions to solve these problems.

So far all I seem find to fixing these problems is by using a synchronizing method. Are there other alternatives to using a synchronizing method?

I have found most of this information from the sun Java tutorial. Any recommendations on further reading?

share|improve this question

closed as off-topic by gnat, Kilian Foth, Bart van Ingen Schenau, MichaelT, Robert Harvey Mar 6 at 20:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Programmers as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – gnat, Kilian Foth, Bart van Ingen Schenau, MichaelT, Robert Harvey
If this question can be reworded to fit the rules in the help center, please edit the question.

4 Answers 4

I would recommend looking into using immutable objects for shared data. As much as possible, try to restrict access to mutated data to a single thread.

Some of the advantages:

  • no problems with memory consistency or thread interference since the shared data can't be changed
  • no need to use synchronized methods

Possible disadvantages:

  • can't update an immutable object
  • less efficient if you have to often create new copies instead of updating
share|improve this answer

You should check out the java.util.concurrent package of Java SE. There are plenty of options on keeping things synchronized. If that doesn't work for you, Google Guava has some interesting additions to this package.

share|improve this answer

Have you had a look at atomic variables?

http://www.ibm.com/developerworks/java/library/j-jtp11234/

The author of the article Brian Geotz also wrote a very good book "Java Concurrency in practice"

share|improve this answer

There are many techniques which don't require explicit synchronisation.

For example, if you keep all data local to each thread and communicate only via passing messages, then the synchronisation is performed implicitly by sending and awaiting messages.

Message passing systems have the advantage that they scale very well, since they don't require a unified memory architecture and there are formal techniques for detecting and preventing deadlock, for instance Communicating Sequential Processes (CSP) helps you formally reason about such systems.

Another option is the use of immutable data sets. If nothing can change, you never need to synchronise, because there will never be any chance that more than one thread will try to write at once.

You can even combine them, if one process creates immutable shared memory data sets, it can use messages to pass pointers to consumers of that data.

Finally, you could think in terms of higher levels of abstractions. Tie your processes together by BlockingQueue from java.util.concurrent and you don't need to think about the actual implementation of the Queue, you just produce them in with one process and consume them in another.

There is even a lower level of abstraction than synchronized, using primitives such as Semaphor.

In all, it is a rich and deep area to study, and understanding the pros and cons of each technique has the potential to make you a better programmer.

share|improve this answer
    
I'll add to this that if you are continuing to use Java's core libraries, then please use java.util.concurrent based techniques and data structures as outlined in Brian Goetz's "Java Concurrency in Practice" title. Using synchronized keyword in a majority of cases is no longer recommended –  Martijn Verburg Feb 29 '12 at 22:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.