14

I guess there is no equivalent of task parallel libraries (of .NET 4.0) in Java. Is that true? What are the improvements that this feature of .NET offer that Java concurrency doesn't.

Nemo
  • 24,540
  • 12
  • 45
  • 61

4 Answers4

13

Java has the java.util.concurrent package, and there's also the fork/join framework. Fork/join is scheduled for inclusion in Java 7, but can be downloaded now and used with Java 6.

A good book for getting to grips with concurrency in Java is Java Concurrency in Practice, by Brian Goetz and others.

Richard Fearn
  • 25,073
  • 7
  • 56
  • 55
  • 1
    Does fork/join framework has anything to do with task parallel library? – Nemo Nov 07 '10 at 12:31
  • 6
    I'm not a .NET developer, but from what I read about TPL (for instance, this: http://msdn.microsoft.com/en-us/library/dd460717.aspx), fork/join does seem to have a lot in common with TPL. For example, "The TPL scales the degree of concurrency dynamically to most efficiently use all the processors that are available." In comparison, this is from the fork/join documentation: "Candidates for fork/join processing mainly include those that can be expressed using parallel divide-and-conquer techniques: ...break it in two (or more) parts, and then solve those parts in parallel" – Richard Fearn Nov 07 '10 at 12:48
  • The sample chapter available through the link is really nice. It made me want to get the book, but it was last published in 2006 so I am not sure how up-to-date it is. – Philippe Jun 24 '14 at 14:54
  • This answer is right on. I would add to that to look at JDeferred or the Bolts framework if you are interested in composition using promises and futures (i.e. this then that). – Philippe Jun 24 '14 at 17:13
  • Is there a 2019 (a few days away) update for a good book? Thank you. – granadaCoder Dec 20 '18 at 16:01
4

Habanero-Java library (HJ-lib) is the new library implementation of Habanero-Java (HJ), a pedagogic parallel programming model being developed at Rice University. HJ-lib is capable of expressing many different forms of parallel patterns including data parallelism, pipeline parallelism, stream parallelism, loop parallelism, and divide-and-conquer parallelism.

HJ-lib integrates a wide range of parallel programming constructs (e.g., async tasks, futures, data-driven tasks, forall, barriers, phasers, transactions, actors) in a single programming model that enables unique combinations of these constructs (e.g., nested combinations of task and actor parallelism).

HJ-lib is built using lambda expressions and can run on any Java 8 JVM. Older JVMs can be targeted by relying on external bytecode transformations tools for compatibility. The HJ runtime is responsible for orchestrating the creation, execution, and termination of HJ tasks, and features both work-sharing and work-stealing schedulers.

HJ-lib is also an attractive tool for educators with numerous educational resources available from the sophomore-level COMP 322 course offered at Rice University. These resources can also be used to learn about the library API. Javadoc for the API is also available.

Here is a simple HelloWorld version:

import static edu.rice.hj.Module1.*;

public class HelloWorld {

    public static void main(final String[] args) {

        launchHabaneroApp(() -> {

            finish(() -> {
                async(() -> System.out.println("Hello"));
                async(() -> System.out.println("World"));
                async(() -> System.out.println("in"));
                async(() -> System.out.println("HJ-lib"));
            });

        });
    }
}

Further examples for the various parallel constructs are available from the COMP 322 course website.

shams
  • 3,460
  • 24
  • 24
2

From what I know there is no equivalent in Java.

I wrote a Java Task library inspired on TPL. It doesn't support all features of TPL but fitted my requirements at that time.

Github: https://github.com/BrunoMNDantas/TPL4J

Maven: https://mvnrepository.com/artifact/com.github.brunomndantas/tpl4j

Bruno Dantas
  • 31
  • 1
  • 3
  • 9
0

Yes. Java has no equivalent of task parallel library - TPL (of .NET 4.0). You are right in your question (main word - NET 4.0). TPL is "shared" - universal unified .NET library for ANY .NET application. It means that you can have single shared async logic for any .NET application (WPF, WinForms, Xamarin.Forms, ASP.NET, ...) with really async (non blocking UI) approach. Java has no. JavaFX has special non blocking Task class, Swing - background workers, Android - own approach. ... So named util.concurrency Java package has no any non freezable UI (JavaFX, Swing) solutions.

Sergey Orlov
  • 491
  • 5
  • 16