This is the 28th day of my participation in the Gwen Challenge.More article challenges
Preface:
Hello, Aribard. Today we are looking at the Phaser implementation of the Java Multithreading toolkit java.util.Concurrent. We are looking at the Phaser implementation of the Java.util.concurrent toolkit. Phaser stands for “stager” and is used to solve the problem of controlling multiple threads to work together in phases. Phaser, an advanced version of CountDownLatch and CyclicBarrier, is more powerful and flexible to use in more scenarios.
Portal:
- The principle and application scenario of CountDownLatch are introduced
- CyclicBarrier principle and application scenarios
- Sano1100this section describes the mechanism and application scenarios
Phaser:
Introduction:
Java 7 introduces a new flexible thread synchronization mechanism called Phaser. Phaser is a good choice if you need to wait for the thread to finish and then continue with other tasks. He introduced the concept of a new Phaser, which we can think of as phases, each of which has a threaded task that needs to be executed before moving on to the next phase. Phaser is therefore particularly well suited for repeated execution or reuse situations.
A Phaser is an advanced version of a CyclicBarrier. Here’s a similar example:
When friends are here to order, can be together for a drink first, each all after some drink, into the next phase in each point to the hot food, after some hot food, each point in a dessert this not too much, and then enter the next phase in each have a dessert, this is the Phaser stage;
Examples of code (I’ve written the code together for simplicity, but keep each method separate when you write it) :
package com.wechat.wechatservice.controller.Executor; import com.sun.tools.internal.jxc.SchemaGenerator; import java.util.concurrent.Phaser; /** * TODO ** @author taoze * @version 1.0 * @date 6/28/21 2:33pm */ Public Class PhaserTest extends Phaser { @override protected Boolean onAdvance(int phase, int registeredParties) {Override protected Boolean onAdvance(int phase, int registeredParties) { return daoqi(); case 1: return yingping(); case 2: return zhucai(); case 3: return tiandian(); default: return true; }} private Boolean tiandian() {system.out.println (); + getRegisteredParties()); return false; } private Boolean zhucai() {system.out.println (" I'm done! + getRegisteredParties()); return false; } private Boolean yingping() {system.out.println () {private Boolean yingping() {system.out.println (); + getRegisteredParties()); return false; } private Boolean daoqi() {system.out.println (" + getRegisteredParties()); return false; } static class Runner implements Runnable { private Phaser phaser; public Runner(Phaser phaser) { this.phaser = phaser; } @override public void run() {system.out.println (" "+ thread.currentThread ().getName()+": "); / * * * to perform this method will after all the contestants finished to the following method * / phaser arriveAndAwaitAdvance (); System.out.println(" student -"+ thread.currentThread ().getName()+": "); phaser.arriveAndAwaitAdvance(); System.out.println(" student -"+ thread.currentThread ().getName()+": "); phaser.arriveAndAwaitAdvance(); System.out.println(" student -"+ thread.currentThread ().getName()+": ordered a chocolate sundae "); phaser.arriveAndAwaitAdvance(); } } public static void main(String[] args) { int runnerNum = 4; PhaserTest phaser = new PhaserTest(); /** * Register (); /** * Register (); for (int i = 0; i < runnerNum; I++) {/** * register once to indicate the number of threads maintained by phaser */ phaser.register(); new Thread(new Runner(phaser)).start(); } / * * * later stages the main thread is not participated in the * / phaser arriveAndDeregister (); }}Copy the code
Execution Result:
Boolean onAdvance(int phase, int registeredParties) is an important Phaser method that often needs to be overridden. This method does two things:
- This method is called automatically when each phase completes, so code written by overloading this method is executed at the end of each phase, the equivalent of a CyclicBarrier’s barrierAction.
- When this method returns true, it means the Phaser has been terminated, so you can smartly set the return value of this method to terminate all threads.
Phaser phase and party two important status: phase, phase said party said the number of threads in each stage, each thread execute the Phaser only. ArriveAndAwaitAdvance () into the next phase, otherwise blocked waiting for;
Ok! That’s all for today’s article. These are the simple ways to use Phaser, and you can change them according to your business. I hope this will help you.
Neat makes for great code, and there’s only so much detail