Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallelism in Java

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

like image 339
Kamahire Avatar asked Dec 01 '22 04:12

Kamahire


2 Answers

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.

like image 58
Peter Lawrey Avatar answered Dec 04 '22 01:12

Peter Lawrey


there is a good book for this topic :

Java Concurrency in Practice

Java Concurrency in Practice

like image 22
Eko Kurniawan Khannedy Avatar answered Dec 04 '22 01:12

Eko Kurniawan Khannedy