当前位置:网站首页>Using completable future to implement asynchronous callback

Using completable future to implement asynchronous callback

2022-07-22 01:26:00 Rear cut

Interface modification based on the example of the boss :https://blog.csdn.net/it_freshman/article/details/80580369

There are several steps to boil a pot of boiling water :

  1. Add water
  2. Turn on the gas
  3. Boil water ing—( More time-consuming actions )
  4. Observe , Boiling water is finished
  5. Finally, after the water boils , Turn off the gas
  6. In the process of boiling water , Don't want to wait , Just opened a game

First, define a water boiling interface :

public interface BoilWater {

     void a_addWater() ;

     void b_on() ;

     void c_boiling() throws Exception ;

     void d_off() ;

//     Water boiling method 
      void make() throws Exception;

     void playGame() throws Exception ;

}

Next, we implement the interface , This time, we use abstract classes to implement this interface , such make Methods can be implemented without specific :

public abstract class AbstractBoilWater implements BoilWater {

    protected volatile boolean isReadyFlag = false;

     @Override
    public void a_addWater() {
        System.out.println("1. Add water ");
    }
    @Override
    public void b_on() {
        System.out.println("2. Turn on the gas ");
    }
    @Override
    public void c_boiling() throws Exception {
        System.out.println("3-1. In boiling water .....");
        Thread.sleep(5000);
        System.out.println("3-2. The water is boiling ");
        isReadyFlag = true;
    }
    @Override
    public void d_off() {
        System.out.println("4. Turn off the gas ");
    }

    // Water boiling method 
    @Override
    public abstract void make() throws Exception;
    @Override
    public void playGame() throws Exception {
        Thread.sleep(1200);
        if (!isReadyFlag) {
            System.out.println(" The water is not boiling yet , Play a game ");
        }
    }

}

Next, call the business , When the water is not good, take the opportunity to play a game , Use CompletableFuture Inherit AbstractBoilWater To make asynchronous calls and listen

public class CompletableFutureBiolWater extends AbstractBoilWater {
    @Override
    public void make() throws Exception {
        a_addWater();
        b_on();
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            try {
                c_boiling();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 1;
        });

        // Registration events “ monitor ”
        future.whenComplete((v, e) -> {
			System.out.println(v);
			System.out.println(e);
            d_off();
        });

        while (!future.isDone()) {
            playGame();
        }
    }
}

Write a test class to test

public class testMain {
    public static void main(String[] args) {
        CompletableFutureBiolWater completableFutureBiolWater= new CompletableFutureBiolWater();
        try {
            completableFutureBiolWater.make();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The effect is as follows :

  remarks , If it's not just the process of boiling water , Maybe boiling water and cutting firewood can be done at the same time , Then it can be transformed into

public class CompletableFutureBiolWater extends AbstractBoilWater {
    @Override
    public void make() throws Exception {
        a_addWater();
        b_on();
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            try {
                c_boiling();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 1;
        });

        // Registration events “ monitor ”
        future.whenComplete((v, e) -> {
			System.out.println(v);
			System.out.println(e);
            d_off();
        });
       // Simulate cutting firewood 
        CompletableFuture<Integer> futurekanchai = CompletableFuture.supplyAsync(() -> {
            try {
                c_boiling();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 2;
        });

        // Registration events “ monitor ”
        futurekanchai.whenComplete((v, e) -> {
            System.out.println(v);
            System.out.println(e);
            d_off();
        });

        while (!future.isDone()&&!futurekanchai.isDone()) {
            playGame();
        }
    }
}

It leads to thinking : In the work order process , Maybe our order actually goes through several processes , At this time, we can use the idea of asynchronous process to design , Reduce synchronization blocking time ; The purpose of defining interfaces here is to implement interfaces for methods , Of course, you can also use engineering mode to optimize

原网站

版权声明
本文为[Rear cut]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/203/202207210705138957.html