Parallel programming in Java

后端 未结 19 1144
时光说笑
时光说笑 2021-01-30 11:14

How can we do Parallel Programming in Java? Is there any special framework for that? How can we make the stuff work?

I will tell you guys what I need, think that I devel

19条回答
  •  日久生厌
    2021-01-30 11:22

    There is a library called Habanero-Java (HJ), developed at Rice University that was built using lambda expressions and can run on any Java 8 JVM.

    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).

    The HJ runtime is responsible for orchestrating the creation, execution, and termination of HJ tasks, and features both work-sharing and work-stealing schedulers. You can follow the tutorial to set it up on your computer.

    Here is a simple HelloWorld example:

    import static edu.rice.hj.Module1.*;
    
    public class HelloWorld {
    
    public static void main(final String[] args) {
    
        launchHabaneroApp(() -> {
    
            finish(() -> {
                async(() -> System.out.println("Hello World - 1!"));
                async(() -> System.out.println("Hello World - 2!"));
                async(() -> System.out.println("Hello World - 3!"));
                async(() -> System.out.println("Hello World - 4!"));
            });
    
        });
    }}
    

    Each async method runs in parallel with the other async methods, while the content within these methods run sequentially. The program doesn't continue until all code within the finish method complete.

提交回复
热议问题