Solved

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

Posted on 2011-09-07
3
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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