java 并发阶段任务中的阶段切换

Phaser类提供了onAdvance()方法,它在Phaser阶段改变时会被自动执行。onAdvance()方法需要两个int类型参数:当前的阶段数和注册的参与者数量。返回的是boolean类型,如果返回的是false,表示phaser继续在执行。如果返回true。则表示phaser类已经执行完成被进入最终态。以下是一个范例。
1.创建Myphaser类并继承Phaser类。
覆盖onAdvance()方法。根据传入phaser的值不同调用不同的方法。

import java.util.concurrent.Phaser;
public class MyPhaser extends Phaser{
    protected boolean onAdvance(int phase,int registeredParties){
        switch (phase) {
        case 0:
            return sudentsArrived();
        case 1:
            return finishFirstExercise();
        case 2:
            return finishSecondExercise();
        case 3:
            return finishExam();
        default:
            return true;
        }
    }
    //打印两条信息到控制台,并返回false表示phaser已经开始执行。
    private boolean sudentsArrived(){
        System.out.printf("Phaser: The exam are going to start."
                + "The students are ready.\n");
        System.out.printf("Phaser: we have %d students.\n",getRegisteredParties());
        return false;
    }
    //打印两条信息到控制台,并返回false表示phaser继续执行。
    private boolean finishFirstExercise(){
        System.out.printf("Phaser: all the student have finished the first exercise.\n");
        System.out.printf("Phaser:it is time for second exercise.\n");
        return false;
    }
    //打印两条信息到控制台,并返回false表示phaser继续执行。
    private boolean finishSecondExercise(){
        System.out.printf("Phaser: all the student have finished the second exercise.\n");
        System.out.printf("Phaser:it is time for third exercise.\n");
        return false;
    }
    //打印两条信息到控制台,并返回true表示phaser已经执行完成。
    private boolean finishExam(){
        System.out.printf("Phaser: All the students have finished the exam.\n");
        System.out.printf("phaser:thank you for your time.\n");
        return false;
    }
}

2.创建Student类,并实现Runnable接口。

package Thread37;

import java.util.Date;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

public class Student implements Runnable {
    private Phaser phaser;
    public Student(Phaser phaser){
        this.phaser = phaser;
    }
    @Override
    public void run() {
        System.out.printf("%s: Has arrived to do exam.\n",Thread.currentThread().getName(),new Date());
        phaser.arriveAndAwaitAdvance();
        System.out.printf("%s: Is going to do the first exercise. %s\n",Thread.currentThread().getName(),new Date());
        doExercise1();
        System.out.printf("%s: Has done the first exercise. %s\n",Thread.currentThread().getName(),new Date());
        phaser.arriveAndAwaitAdvance();
        System.out.printf("%s Is going to do the second exercixe.%s\n",Thread.currentThread().getName(),new Date());
        doExercise2();
        System.out.printf("%s: Has done the second exercise.%s\n",Thread.currentThread().getName(),new Date());
        phaser.arriveAndAwaitAdvance();
        System.out.printf("%s: Is going to do the third exercise %s.\n",Thread.currentThread().getName(),new Date());
        doExercise3();
        System.out.printf("%s :Has finished the exam.%s\n",Thread.currentThread().getName(),new Date());
        phaser.arriveAndAwaitAdvance();
    }

    private void doExercise1(){ 
        try {
            long duration = (long)(Math.random()*10);
            TimeUnit.SECONDS.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }   

    private void doExercise2(){ 
        try {
            long duration = (long)(Math.random()*10);
            TimeUnit.SECONDS.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void doExercise3(){ 
        try {
            long duration = (long)(Math.random()*10);
            TimeUnit.SECONDS.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

最后实现了Main类。

package Thread37;

public class Main {

    public static void main(String[] args) {
        MyPhaser myPhaser = new MyPhaser();
        Student student[] = new Student[5];
        for(int i=0;i<5;i++){
            student[i] = new Student(myPhaser);
            myPhaser.register();
        }
        Thread threads[] = new Thread[5];
        for(int i=0;i<5;i++){
            threads[i] = new Thread(student[i],"Student"+i);
            threads[i].start();
        }
        for(int i=0;i<5;i++){
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }

        System.out.printf("Main: The phaser has finished:%s.\n",myPhaser.isTerminated());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值