Equivalent of Task Parallel Library in Java

2019-03-11 07:22发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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.