Solved

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

Posted on 2011-09-07
3
270 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

729 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