`

Concurrent - Phaser - 操纵Parties的常用API

 
阅读更多

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

 

getRegisteredParties()

getRegisteredParties()作用是获得注册的parties数量

register()

register()作用是动态添加一个parties值


PhaserTest6.java

package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

public class PhaserTest6 {
    public static void main(String[] args) {
        Phaser phaser = new Phaser(5);
        System.out.println(phaser.getRegisteredParties());

        phaser.register();
        System.out.println(phaser.getRegisteredParties());

        phaser.register();
        System.out.println(phaser.getRegisteredParties());

        phaser.register();
        System.out.println(phaser.getRegisteredParties());

        phaser.register();
        System.out.println(phaser.getRegisteredParties());

        phaser.register();
        System.out.println(phaser.getRegisteredParties());
    }
}

Run


 

bulkRegister(int parties)

bulkRegister(int parties)作用是批量增加parties的数量


PhaserTest10.java

package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

public class PhaserTest10 {
    public static void main(String[] args) {
        Phaser phaser = new Phaser(5);
        System.out.println(phaser.getRegisteredParties());

        phaser.bulkRegister(10);
        System.out.println(phaser.getRegisteredParties());

        phaser.bulkRegister(10);
        System.out.println(phaser.getRegisteredParties());

        phaser.bulkRegister(10);
        System.out.println(phaser.getRegisteredParties());

        phaser.bulkRegister(10);
        System.out.println(phaser.getRegisteredParties());
    }
}

Run 


 

getArrivedParties()

getArrivedParties()作用是获得已经被使用的parties的个数

getUnarrivedParties()

getUnarrivedParties()作用是获得未被使用的parties的个数


PhaserTest7.java

package org.fool.java.concurrent.phaser;

import java.util.concurrent.Phaser;

public class PhaserTest7 {
    public static class MyThread implements Runnable {
        private Phaser phaser;

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

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

            phaser.arriveAndAwaitAdvance();

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

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

            for (int i = 0; i < 8; i++) {
                Thread thread = new Thread(new MyThread(phaser));
                thread.setName("Thread " + (i + 1));
                thread.start();
            }

            Thread.sleep(3000);

            System.out.println(phaser.getArrivedParties());
            System.out.println(phaser.getUnarrivedParties());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Run


 

Reference

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

 

 

 

  • 大小: 4.2 KB
  • 大小: 3.3 KB
  • 大小: 12.6 KB
  • 大小: 4.8 KB
  • 大小: 12.3 KB
  • 大小: 5.4 KB
  • 大小: 6.3 KB
  • 大小: 22 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics