Example usage for java.util.concurrent Phaser Phaser

List of usage examples for java.util.concurrent Phaser Phaser

Introduction

In this page you can find the example usage for java.util.concurrent Phaser Phaser.

Prototype

public Phaser(Phaser parent) 

Source Link

Document

Equivalent to #Phaser(Phaser,int) Phaser(parent, 0) .

Usage

From source file:StartTogetherTask.java

public static void main(String[] args) {
    Phaser phaser = new Phaser(1);
    for (int i = 1; i <= 3; i++) {
        phaser.register();/* ww w .  j  a v  a  2  s.com*/
        String taskName = "Task  #" + i;
        StartTogetherTask task = new StartTogetherTask(taskName, phaser);
        task.start();
    }
    phaser.arriveAndDeregister();
}

From source file:Entity.java

private static 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);
        phaser.register();//from  w  ww  .jav a  2  s.  c o m
        new Thread() {
            @Override
            public void run() {
                System.out.println(member);
                phaser.arriveAndAwaitAdvance();
                entity.run();
            }
        }.start();
    }
    phaser.arriveAndDeregister();
}

From source file:ipLock.ProcessHandle.java

public ProcessHandle(WorkerBreakpoint breakpoint) {
    this.id = WorkerProcessId.next();
    this.currentBreakpoint = breakpoint;

    this.phaser = new Phaser(2) {

        @Override//from  ww  w .  j a va2  s.  c  om
        protected boolean onAdvance(int phase, int registeredParties) {
            LOGGER.info("process {} reached breakpoint {}", id, currentBreakpoint.name());
            currentBreakpoint = null;

            return false;
        }
    };

    this.pb = new ProcessBuilder(javaExecutablePath, "-classpath", javaClasspath, Worker.class.getName());
    this.pb.inheritIO();

    putEnv(WorkerEnv.ID, id);
}

From source file:io.getlime.push.service.PushMessageSenderService.java

/**
 * Send push notifications to given application.
 *
 * @param appId App ID used for addressing push messages. Required so that appropriate APNs/FCM credentials can be obtained.
 * @param pushMessageList List with push message objects.
 * @return Result of this batch sending.
 *///from   w  ww . j a  v a  2s  . c o m
@Transactional
public PushMessageSendResult sendPushMessage(final Long appId, List<PushMessage> pushMessageList)
        throws PushServerException {
    // Prepare clients
    AppRelatedPushClient pushClient = prepareClients(appId);

    // Prepare synchronization primitive for parallel push message sending
    final Phaser phaser = new Phaser(1);

    // Prepare result object
    final PushMessageSendResult sendResult = new PushMessageSendResult();

    // Send push message batch
    for (PushMessage pushMessage : pushMessageList) {

        // Validate push message before sending
        validatePushMessage(pushMessage);

        // Fetch connected devices
        List<PushDeviceRegistrationEntity> devices = getPushDevices(appId, pushMessage.getUserId(),
                pushMessage.getActivationId());

        // Iterate over all devices for given user
        for (final PushDeviceRegistrationEntity device : devices) {
            final PushMessageEntity pushMessageObject = pushMessageDAO.storePushMessageObject(
                    pushMessage.getBody(), pushMessage.getAttributes(), pushMessage.getUserId(),
                    pushMessage.getActivationId(), device.getId());

            // Check if given push is not personal, or if it is, that device is in active state.
            // This avoids sending personal notifications to devices that are blocked or removed.
            boolean isMessagePersonal = pushMessage.getAttributes() != null
                    && pushMessage.getAttributes().getPersonal();
            boolean isDeviceActive = device.getActive();
            if (!isMessagePersonal || isDeviceActive) {

                // Register phaser for synchronization
                phaser.register();

                // Decide if the device is iOS or Android and send message accordingly
                String platform = device.getPlatform();
                if (platform.equals(PushDeviceRegistrationEntity.Platform.iOS)) {
                    sendMessageToIos(pushClient.getApnsClient(), pushMessage.getBody(),
                            pushMessage.getAttributes(), device.getPushToken(),
                            pushClient.getAppCredentials().getIosBundle(), new PushSendingCallback() {
                                @Override
                                public void didFinishSendingMessage(Result result,
                                        Map<String, Object> contextData) {
                                    switch (result) {
                                    case OK: {
                                        sendResult.getIos().setSent(sendResult.getIos().getSent() + 1);
                                        pushMessageObject.setStatus(PushMessageEntity.Status.SENT);
                                        pushMessageDAO.save(pushMessageObject);
                                        break;
                                    }
                                    case PENDING: {
                                        sendResult.getIos().setPending(sendResult.getIos().getPending() + 1);
                                        pushMessageObject.setStatus(PushMessageEntity.Status.PENDING);
                                        pushMessageDAO.save(pushMessageObject);
                                        break;
                                    }
                                    case FAILED: {
                                        sendResult.getIos().setFailed(sendResult.getIos().getFailed() + 1);
                                        pushMessageObject.setStatus(PushMessageEntity.Status.FAILED);
                                        pushMessageDAO.save(pushMessageObject);
                                        break;
                                    }
                                    case FAILED_DELETE: {
                                        sendResult.getIos().setFailed(sendResult.getIos().getFailed() + 1);
                                        pushMessageObject.setStatus(PushMessageEntity.Status.FAILED);
                                        pushMessageDAO.save(pushMessageObject);
                                        pushDeviceRepository.delete(device);
                                        break;
                                    }
                                    }
                                    sendResult.getIos().setTotal(sendResult.getIos().getTotal() + 1);
                                    phaser.arriveAndDeregister();
                                }
                            });
                } else if (platform.equals(PushDeviceRegistrationEntity.Platform.Android)) {
                    final String token = device.getPushToken();
                    sendMessageToAndroid(pushClient.getFcmClient(), pushMessage.getBody(),
                            pushMessage.getAttributes(), token, new PushSendingCallback() {
                                @Override
                                public void didFinishSendingMessage(Result sendingResult,
                                        Map<String, Object> contextData) {
                                    switch (sendingResult) {
                                    case OK: {
                                        sendResult.getAndroid().setSent(sendResult.getAndroid().getSent() + 1);
                                        pushMessageObject.setStatus(PushMessageEntity.Status.SENT);
                                        pushMessageDAO.save(pushMessageObject);
                                        updateFcmTokenIfNeeded(appId, token, contextData);
                                        break;
                                    }
                                    case PENDING: {
                                        sendResult.getAndroid()
                                                .setPending(sendResult.getAndroid().getPending() + 1);
                                        pushMessageObject.setStatus(PushMessageEntity.Status.PENDING);
                                        pushMessageDAO.save(pushMessageObject);
                                        break;
                                    }
                                    case FAILED: {
                                        sendResult.getAndroid()
                                                .setFailed(sendResult.getAndroid().getFailed() + 1);
                                        pushMessageObject.setStatus(PushMessageEntity.Status.FAILED);
                                        pushMessageDAO.save(pushMessageObject);
                                        break;
                                    }
                                    case FAILED_DELETE: {
                                        sendResult.getAndroid()
                                                .setFailed(sendResult.getAndroid().getFailed() + 1);
                                        pushMessageObject.setStatus(PushMessageEntity.Status.FAILED);
                                        pushMessageDAO.save(pushMessageObject);
                                        pushDeviceRepository.delete(device);
                                        break;
                                    }
                                    }
                                    sendResult.getAndroid().setTotal(sendResult.getAndroid().getTotal() + 1);
                                    phaser.arriveAndDeregister();
                                }
                            });
                }
            }
        }
    }
    phaser.arriveAndAwaitAdvance();
    return sendResult;
}