Using the reusable synchronization barrier Phaser - Java Thread

Java examples for Thread:Phaser

Description

Using the reusable synchronization barrier Phaser

Demo Code

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Phaser;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

public class Main {

    private static abstract class Entity implements Runnable {
        public abstract void run();
    }/*from w ww  .j  ava2 s.c o m*/

    private static class Player extends Entity implements Runnable {
        private final static AtomicInteger idSource = new AtomicInteger();
        private final int id = idSource.incrementAndGet();

        public void run() {
            System.out.println(toString() + " started");
            try {
                Thread.currentThread().sleep(
                        ThreadLocalRandom.current().nextInt(200, 600));
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            System.out.println(toString() + " stopped");
        }

        @Override
        public String toString() {
            return "Player #" + id;
        }
    }

    private static class Zombie extends Entity implements Runnable {
        private final static AtomicInteger idSource = new AtomicInteger();
        private final int id = idSource.incrementAndGet();

        public void run() {
            System.out.println(toString() + " started");
            try {
                Thread.currentThread().sleep(
                        ThreadLocalRandom.current().nextInt(400, 800));
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            System.out.println(toString() + " stopped");
        }

        @Override
        public String toString() {
            return "Zombie #" + id;
        }
    }
    
    private void execute() {
        List<Entity> entities = new ArrayList<>();
        entities = new ArrayList<>();
        entities.add(new Player());
        entities.add(new Zombie());
        entities.add(new Zombie());
        entities.add(new Zombie());
        gameEngine(entities);
    }
    
    private void gameEngine(List<Entity> entities) {
        final Phaser phaser = new Phaser(1);
        for (final Entity entity : entities) {
            final String member = entity.toString();
            System.out.println(member + " joined the game");
            phaser.register();
            new Thread() {
                @Override
                public void run() {
                    System.out.println(member + 
                            " waiting for the remaining participants");
                    phaser.arriveAndAwaitAdvance(); // wait for remaining entities
                    System.out.println(member + " starting run");
                    entity.run();
                }
            }.start();
        }
        phaser.arriveAndDeregister();     //Deregister and continue
        System.out.println("Phaser continuing");
  

    }

    public static void main(String[] args) {
        new UsingTheReusableSynchronizationBarrierPhaser().execute();
    }

}

Related Tutorials