Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java parallel execution on multicore

i would like to know whether exist a way to parallelize queries in Java (or there is framework or a library) like in C# and Linq:

var query = from item in source.AsParallel().WithDegreeOfParallelism(2)
        where Compute(item) > 42
        select item;

and if i can't parallelize queries if i can do something like this in c# (a for each parallelized) :

  Parallel.ForEach(files, currentFile =>
        {
            // The more computational work you do here, the greater  
            // the speedup compared to a sequential foreach loop. 
            string filename = System.IO.Path.GetFileName(currentFile);
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(currentFile);

            bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            bitmap.Save(System.IO.Path.Combine(newDir, filename));

            // Peek behind the scenes to see how work is parallelized. 
            // But be aware: Thread contention for the Console slows down parallel loops!!!
            Console.WriteLine("Processing {0} on thread {1}", filename,
                                Thread.CurrentThread.ManagedThreadId);

        } 

please if you post any framework or library, can you tell me the experience that you had with it ? thx for your time.

about c# and linq you can find here the documentation : http://msdn.microsoft.com/en-us/library/dd997425.aspx

like image 763
T-student Avatar asked Sep 14 '26 23:09

T-student


1 Answers

There isn't a direct translation. Firstly Java doesn't have LINQ nor does it have any standard parallel collection classes.

GPars is perhaps closest fit.

Note that while it is targeted at groovy it's API is perfectly usable from java

like image 121
Gareth Davis Avatar answered Sep 17 '26 11:09

Gareth Davis