Solved

Use of Java.Util.Concurrent in multi-core server

Posted on 2011-09-07
3
266 Views
Last Modified: 2012-05-12
Hi:

What are things you have to take care of during multi-threaded programming using java.util.concurrent (java.util.concurrent package for Java 7) on multi core server.

Please let me know any thread safety issue's due in use of this package on multi-core or multi processor server.
 
Best Regards,

-Sunnybrad
0
Comment
Question by:sunnybrad
  • 2
3 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36499763
0
 
LVL 10

Expert Comment

by:gordon_vt02
ID: 36504615
Take a look at the book Java Concurrency in Practice by Brian Goetz.  It's a great reference for concurrent programming and explains a lot of methods for making objects thread-safe.

http://jcip.net/
0
 
LVL 10

Accepted Solution

by:
gordon_vt02 earned 500 total points
ID: 36504671
A quick summary of things to watch out for:

You need to make sure that any objects you are sharing among multiple threads are thread-safe.  The easiest way to do this is to make them immutable.  If the state of the object can't change, there are no issues sharing it among multiple threads.

If shared objects must be changed by one or more threads, you have several options:
- Use the built-in synchronized keyword on the accessor and mutator methods for the shared state
- Create a private final Object in your class that is used to lock only the code blocks that mutate the object.  (synchronized (lockObject) { // mutation code })
- Use the java.util.concurrent.AtomicXxx classes to provide atomic operations for ints, booleans, and other common types.
- Use the concurrency uttilities (Locks, Mutexes, etc.) in java.util.concurrent to handle explicit locking/unlocking of critical sections of your code.

There are other options as well, such as the volatile keyword, but I'd recommend reading one of the references out there -- JCIP being a great one -- for all the details and potential pitfalls.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now