Concurrent - Phaser - awaitAdvance & awaitAdvanceInterruptibly

原创转载请注明出处:http://agilestyle.iteye.com/blog/2344661

 

awaitAdvance(int phase)

awaitAdvance(int phase)作用是如果传入参数phase值和当前getPhase()返回值一样,则在屏障处等待,否则立刻返回执行下面的代码


PhaserTest11.java

package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

public class PhaserTest11 {
    public static class ThreadA implements Runnable {
        private Phaser phaser;

        public ThreadA(Phaser phaser) {
            this.phaser = phaser;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " A1 begin " + System.currentTimeMillis());

            phaser.arriveAndAwaitAdvance();

            System.out.println(Thread.currentThread().getName() + " A1 end " + System.currentTimeMillis());
        }
    }

    public static class ThreadB implements Runnable {
        private Phaser phaser;

        public ThreadB(Phaser phaser) {
            this.phaser = phaser;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " A1 begin " + System.currentTimeMillis());

            phaser.arriveAndAwaitAdvance();

            System.out.println(Thread.currentThread().getName() + " A1 end " + System.currentTimeMillis());
        }
    }

    public static class ThreadC implements Runnable {
        private Phaser phaser;

        public ThreadC(Phaser phaser) {
            this.phaser = phaser;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " A1 begin " + System.currentTimeMillis());

                Thread.sleep(3000);

                System.out.println("phaser.getPhase()=" + phaser.getPhase());
                phaser.awaitAdvance(0);

                System.out.println(Thread.currentThread().getName() + " A1 end " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class ThreadD implements Runnable {
        private Phaser phaser;

        public ThreadD(Phaser phaser) {
            this.phaser = phaser;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " A1 begin " + System.currentTimeMillis());

                Thread.sleep(5000);

                phaser.arriveAndAwaitAdvance();

                System.out.println(Thread.currentThread().getName() + " A1 end " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Phaser phaser = new Phaser(3);

        Thread t1 = new Thread(new ThreadA(phaser));
        Thread t2 = new Thread(new ThreadB(phaser));
        Thread t3 = new Thread(new ThreadC(phaser));
        Thread t4 = new Thread(new ThreadD(phaser));

        t1.setName("ThreadA");
        t2.setName("ThreadB");
        t3.setName("ThreadC");
        t4.setName("ThreadD");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

Run


Note:

awaitAdvance(int phase)并不参与parties计数的操作,仅仅具有判断的功能

 

更改ThreadC,将 

phaser.awaitAdvance(0);

改为

phaser.awaitAdvance(1);

再Run


Note:

由于传入参数phase值和当前getPhase()返回值不一致,则立即返回

 

awaitAdvanceInterruptibly(int phase)

awaitAdvanceInterruptibly(int phase)作用是传入参数phase值和当前getPhase()返回值不一致时,则继续执行下面的代码

PhaserTest14.java

package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

public class PhaserTest14 {
    public static class ThreadA implements Runnable {
        private Phaser phaser;

        public ThreadA(Phaser phaser) {
            this.phaser = phaser;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " A1 begin " + System.currentTimeMillis());

                System.out.println("phaser.getPhase()=" + phaser.getPhase());
                phaser.awaitAdvanceInterruptibly(10);

                System.out.println(Thread.currentThread().getName() + " A1 end " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                System.out.println("InterruptedException invoked...");
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Phaser phaser = new Phaser(3);

        Thread t1 = new Thread(new ThreadA(phaser));
        t1.setName("ThreadA");
        t1.start();
    }
}

Run


Note:

由于传入参数phase值和当前getPhase()返回值不一致,控制台程序快速继续向下执行,运行的时间几乎是一样的

 

awaitAdvance(int phase)是不可中断的

PhaserTest12.java

package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

public class PhaserTest12 {
    public static class ThreadA implements Runnable {
        private Phaser phaser;

        public ThreadA(Phaser phaser) {
            this.phaser = phaser;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " A1 begin " + System.currentTimeMillis());

            System.out.println("phaser.getPhase()=" + phaser.getPhase());
            phaser.awaitAdvance(0);

            System.out.println(Thread.currentThread().getName() + " A1 end " + System.currentTimeMillis());
        }
    }

    public static void main(String[] args) {
        try {
            Phaser phaser = new Phaser(3);

            Thread t1 = new Thread(new ThreadA(phaser));
            t1.setName("ThreadA");
            t1.start();

            Thread.sleep(3000);

            t1.interrupt();

            System.out.println("interrupt() invoked..");
        } catch (InterruptedException e) {
            System.out.println("InterruptedException invoked...");
            e.printStackTrace();
        }
    }
}

Run


Note:

控制台没有出现异常,说明线程并未中断 

 

awaitAdvanceInterruptibly(int phase)是可中断的

PhaserTest13.java

package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

public class PhaserTest13 {
    public static class ThreadA implements Runnable {
        private Phaser phaser;

        public ThreadA(Phaser phaser) {
            this.phaser = phaser;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " A1 begin " + System.currentTimeMillis());

                System.out.println("phaser.getPhase()=" + phaser.getPhase());
                phaser.awaitAdvanceInterruptibly(0);

                System.out.println(Thread.currentThread().getName() + " A1 end " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                System.out.println("InterruptedException invoked...");
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            Phaser phaser = new Phaser(3);

            Thread t1 = new Thread(new ThreadA(phaser));
            t1.setName("ThreadA");
            t1.start();

            Thread.sleep(5000);

            t1.interrupt();

            System.out.println("interrupt() invoked..");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Run


Note:

控制台出现异常,线程被中断了 

 

awaitAdvanceInterruptibly(int phase, long timeout, TimeUnit unit)

awaitAdvanceInterruptibly(int phase, long timeout, TimeUnit unit)作用是在指定的Phase等待最大的单位时间,如果在指定的时间内,Phase值未变,则出现异常,否则继续向下运行。


PhaserTest15.java

package org.fool.java.concurrent.phaser;

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

public class PhaserTest15 {
    public static class ThreadA implements Runnable {
        private Phaser phaser;

        public ThreadA(Phaser phaser) {
            this.phaser = phaser;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " A1 begin " + System.currentTimeMillis());

                System.out.println("phaser.getPhase()=" + phaser.getPhase());
                phaser.awaitAdvanceInterruptibly(0, 5, TimeUnit.SECONDS);

                System.out.println(Thread.currentThread().getName() + " A1 end " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                System.out.println("InterruptedException invoked...");
                e.printStackTrace();
            } catch (TimeoutException e) {
                System.out.println("TimeoutException invoked...");
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Phaser phaser = new Phaser(3);

        Thread t1 = new Thread(new ThreadA(phaser));
        t1.setName("ThreadA");
        t1.start();
    }
}

Run  


Note:

出现了超时异常,因为5秒之后phase值并没有发生改变 

 

修改main方法

public static void main(String[] args) {
	try {
		Phaser phaser = new Phaser(3);

		Thread t1 = new Thread(new ThreadA(phaser));
		t1.setName("ThreadA");
		t1.start();

		System.out.println("phaser.getArrivedParties()=" + phaser.getArrivedParties());

		Thread.sleep(1000);
		phaser.arrive();
		System.out.println("phaser.getArrivedParties()=" + phaser.getArrivedParties());

		Thread.sleep(1000);
		phaser.arrive();
		System.out.println("phaser.getArrivedParties()=" + phaser.getArrivedParties());

		Thread.sleep(1000);
		phaser.arrive();
		System.out.println("phaser.getArrivedParties()=" + phaser.getArrivedParties());

		Thread.sleep(3000);
		System.out.println("phaser.getPhase()=" + phaser.getPhase());
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}

再Run


Note:

5秒之后phase值发生改变,继续向下运行 

 

再次修改main方法

public static void main(String[] args) {
	try {
		Phaser phaser = new Phaser(3);

		Thread t1 = new Thread(new ThreadA(phaser));
		t1.setName("ThreadA");
		t1.start();

		Thread.sleep(1000);
		t1.interrupt();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}

再Run


Note:

提前将还未到达5秒的线程进行了中断 

 

Reference

Java并发编程核心方法与框架  

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In Verilog, a non-net refers to a variable that is not a wire or a register, but rather a constant or a parameter. Concurrent assignment refers to the assignment of a value to a variable using the "=" operator in a module outside of any procedural blocks (such as always or initial blocks). A concurrent assignment to a non-net is not allowed in Verilog. This is because a non-net does not have a storage element and cannot hold a value assigned to it. Instead, it is typically used as a constant or a parameter that is available for use in the module. To assign a value to a non-net, it should be done within a procedural block using the appropriate assignment operator (such as "<=" for registers or "assign" for wires). Alternatively, the value can be passed as an argument to the module using the parameter keyword. For example: module my_module #(parameter WIDTH = 8) ( input clk, input [WIDTH-1:0] data_in, output [WIDTH-1:0] data_out ); // This is a non-net parameter parameter ADD_VALUE = 5; // This is a register that can be assigned using the "=" operator within an always block reg [WIDTH-1:0] register_data; always @(posedge clk) begin register_data <= data_in + ADD_VALUE; end // This is a wire that can be assigned using the "assign" keyword assign data_out = register_data; endmodule In this example, ADD_VALUE is a non-net parameter that is used in the always block to add a constant value to the input data. The register_data variable is assigned using the "=" operator within the always block. The data_out wire is assigned using the "assign" keyword.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值