3

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

FrankTan
  • 1,626
  • 6
  • 28
  • 63

2 Answers2

1

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

Community
  • 1
  • 1
Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
0

Maybe Fork/Join Framework can help you ? Here is java tutorial

Sergii Zagriichuk
  • 5,389
  • 5
  • 28
  • 45
  • this can be good for recursive tasks but dose not help me to improve the performance of a query and it dose not so simple like to write a keyword as in c# – FrankTan Oct 13 '12 at 15:33
  • and i can't extends the execution to more threads in a simple way so i would like something of more simple to use – FrankTan Oct 13 '12 at 15:35
  • counts of threads != improving of speed, you should understand it, but you can use, just ExecutionService to submit new tasks (queries) as paralel process. – Sergii Zagriichuk Oct 13 '12 at 15:37
  • @frank and judging to this thread http://stackoverflow.com/questions/1077584/parallel-extensions-equivalent-in-java I am right – Sergii Zagriichuk Oct 13 '12 at 15:40
  • F/J isn't really suited for [IO bound tasks](http://stackoverflow.com/questions/8206318/is-javas-fork-and-join-thread-pool-is-good-for-executing-io-bound-task) – btiernay Oct 13 '12 at 15:58