Question

Is there any library like Intel TBB in Java which support Parallelism.

Was it helpful?

Solution

Perhaps you could clarify what exactly you are looking for

Intel® Threading Building Blocks (Intel TBB) offers a rich and complete approach to expressing parallelism in a C++ program. It is a library that helps you take advantage of multi-core processor performance without having to be a threading expert. Intel TBB is not just a threads-replacement library. It represents a higher-level, task-based parallelism that abstracts platform details and threading mechanisms for scalability and performance.

This is what the concurrency libraries have done since 1998 and were made part of Java 5.0 in 2004.

EDIT: Say you want a thread pool which can use all the logical processors on your system.

ExecutorService es = Executors.newFixedThreadPool(
      Runtime.getRuntime().availableProcessors);

// to add a number of tasks
for(int i=0; i<numberOfTasks; i++) {
   es.submit(new Callable<ResultType>() {
      public ResultType call() {
          return doWork(i);
      }
   }
}

This will performs doWork on each free thread.

Looking at its features they look very familiar.

I had a look at some of the low level optimisations like thread aware memory allocation. In Java this is called TLAB (Thread Local Allocation Buffers) and is transparent. I suspect most Java developers don't even know they exist.

Results and Exceptions are captured for you in a Future object which you can inspect later.

You can have "condition variables" such as CountdownLatch or CyclicBarrier

A new container mimicking C++ 0x unordered_map and based on a joint specification implemented by both Intel (TBB 3.0) and Microsoft (Visual Studio 2010). It has three advantages over the previous concurrent_hash_map:

  • An interface closely resembling the C++0x unordered_map
  • It permits concurrent insertion and traversal.
  • No locking is exposed by the interface. An implementation may use locks internally, but the locking is never exposed in a way that can contribute to deadlock. It may hold locks internally, but never while calling user defined code.

Java's ConcurrentHashMap supports the ConcurrentMap and Map interfaces, permits concurrent insertion and traversal and does not expose any locks. ;) It is at least 9 years old so you know it should be robust and stable.

There is a PriorityQueue which you can use in you thread pool if you wish.

OTHER TIPS

there is a good book for this topic :

Java Concurrency in Practice

Java Concurrency in Practice

It is available in the java.util.concurrent package, since Java5.

That edition included a major overhaul of the Java memory model, to solidify the basis of concurrent programming and fix known problems. In Java, concurrency is designed into the language from the start, not simply added later in a library.

Take a look at akka which provides a concurrency framework using Actors and software transactional memory. Of course, this just provides building blocks, you still need to be able to write some concurrent code, but it becomes a little easier.

Check out the java.util.concurrent package. It has a whole bunch of classes that are useful for parallel programming.

By the way, you should consider accepting some answers. 15 questions with no accepted answers discourages people from answering your questions.

Some of you are asking about the difference between concurrency and parallelism. Concurency means different programs that simulate running at the same time and the scheduler decides who is the one is running. Parallelism means one program divided into tasks and those task running at the same time in different cores to solve a problem. When this tasks comptete for resources with other programs (we have more tasks than cores available) then we have concurrency and parallelism at the same time. The avoing race conditions is the less difficult part in parallelism, the big problem is to find a good way to decompose a task properly to get good results. Very fine grained decompostion has drawbacks (overheads) but coarse-grained parallelism (monotasking) has drawbacks too (you are not using all processing power available on the computer). You have to find the medium term, it implies doing some optimization problems (mathematics)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top