Package java.util.concurrent

Examples of java.util.concurrent.CountDownLatch


        if (count == 0) {
            if (latch.getCount() != 0) {
                latch.countDown();
            }
        } else if (latch.getCount() == 0) {
            latch = new CountDownLatch(1);
        }

        this.count = count;
    }
View Full Code Here


    RenderingErrorObserver previousRenderingErrorObserver = getRenderingErrorObserver();
    try {
      // Replace current rendering error observer by a listener that counts down
      // a latch to check further if a rendering error happened during off screen rendering
      // (rendering error listener is called from a notification thread)
      final CountDownLatch latch = new CountDownLatch(1);
      setRenderingErrorObserver(new RenderingErrorObserver() {
          public void errorOccured(int errorCode, String errorMessage) {
            latch.countDown();
          }
        });
     
      // Create an off screen canvas and bind it to view
      offScreenCanvas = getOffScreenCanvas3D(width, height);
      view.addCanvas3D(offScreenCanvas);
     
      // Render off screen canvas
      offScreenCanvas.renderOffScreenBuffer();
      offScreenCanvas.waitForOffScreenRendering();
     
      // If latch count becomes equal to 0 during the past instructions or in the coming 10 milliseconds,
      // this means that a rendering error happened
      if (latch.await(10, TimeUnit.MILLISECONDS)) {
        throw new IllegalRenderingStateException("Off screen rendering unavailable");
      }
     
      return offScreenCanvas.getOffScreenBuffer().getImage();
    } catch (InterruptedException ex) {
View Full Code Here

    public BroadcasterFuture(Future<?> innerFuture, E msg) {
        this.msg = msg;
        this.innerFuture = innerFuture;
        if (innerFuture == null) {
            latch = new CountDownLatch(1);
        } else {
            latch = null;
        }
    }
View Full Code Here

     * @param res    the {@link HttpServletResponse}
     */
    @Override
    protected void suspend(Action action, HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
        CountDownLatch latch = new CountDownLatch(1);

        req.setAttribute(LATCH, latch.hashCode());
        latchs.put(latch.hashCode(), latch);

        if (supportSession()) {
            // Store as well in the session in case the resume operation
            // happens outside the AtmosphereHandler.onStateChange scope.
            req.getSession().setAttribute(LATCH, latch.hashCode());
        }

        try {
            // Google App Engine doesn't allow a thread to block more than 30 seconds
            if (action.timeout != -1 && action.timeout < 30000) {
                latch.await(action.timeout, TimeUnit.MILLISECONDS);
            }
            else {
                latch.await(20000, TimeUnit.MILLISECONDS);
            }
        }
        catch (Throwable ex) {
            logger.warn("Unable to resume the suspended connection", ex);
        }
View Full Code Here

                int latchId = (req.getAttribute(LATCH) == null ? 0 : (Integer)req.getAttribute(LATCH));
                if (req.getSession(true).getAttribute(LATCH) != null) {
                    latchId = (Integer) req.getSession(true).getAttribute(LATCH);
                }
                CountDownLatch latch = latchs.get(latchId);

                if (latch == null && req.getAttribute(AtmosphereResourceImpl.PRE_SUSPEND) == null) {
                    logger.debug("response wasn't suspended: {}", res);
                    return action;
                }

                latch.countDown();
                                                                 
                Action nextAction = resumed(req, res);
                if (nextAction.type == Action.TYPE.SUSPEND) {
                    logger.debug("Suspending after resuming response: {}", res);
                    suspend(action, req, res);
View Full Code Here

     * @throws java.io.IOException
     * @throws javax.servlet.ServletException
     */
    protected void suspend(Action action, HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
        CountDownLatch latch = new CountDownLatch(1);

        int hash = latch.hashCode();
        req.setAttribute(LATCH, hash);
        latchs.put(hash, latch);

        if (supportSession()) {
            // Store as well in the session in case the resume operation
            // happens outside the AtmosphereHandler.onStateChange scope.
            req.getSession().setAttribute(String.valueOf(req.hashCode()), hash);
        }

        try {
            if (action.timeout != -1) {
                latch.await(action.timeout, TimeUnit.MILLISECONDS);
            } else {
                latch.await();
            }
        } catch (InterruptedException ex) {
            logger.debug("", ex);
        } finally {
            latchs.remove(hash);
View Full Code Here

        int latchId = -1;

        if (req.getAttribute(LATCH) != null) {
            latchId = (Integer) req.getAttribute(LATCH);
        }
        CountDownLatch latch = latchs.remove(latchId);
        Action a = super.cancelled(req,res);
        latch.countDown();
        return a;       
    }
View Full Code Here

                    }
                }

                String s = config.getInitParameter(AtmosphereServlet.RESUME_AND_KEEPALIVE);
                if (latchId != -1 && (s == null || s.equalsIgnoreCase("false"))) {
                    CountDownLatch latch = latchs.remove(latchId);
                    if (latch == null) {
                        logger.error("Unable to resume the suspended connection with latchId: {}", latchId);
                    }
                    else {
                        latch.countDown();
                    }
                }
                else if (req.getAttribute(AtmosphereResourceImpl.PRE_SUSPEND) == null) {
                    logger.error("Unable to resume the suspended connection");
                }
View Full Code Here

    @Test(timeOut = 60000)
    public void testResumeOnBroadcast() {
        logger.info("running test: testResumeOnBroadcast");

        final CountDownLatch latch = new CountDownLatch(1);
        servletLatch = new CountDownLatch(1);
        AsyncHttpClient c = new AsyncHttpClient();
        try {
            long currentTime = System.currentTimeMillis();
            final AtomicReference<Response> r = new AtomicReference();
            c.prepareGet(urlTarget).execute(new AsyncCompletionHandler<Response>() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    r.set(response);
                    latch.countDown();
                    return response;
                }
            });

            servletLatch.await();

            Broadcaster b = BroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class, "/*");
            assertNotNull(b);
            b.broadcast("resume").get();

            try {
                latch.await();
            } catch (InterruptedException e) {
                fail(e.getMessage());
                return;
            }

View Full Code Here

    }

    @Test(timeOut = 60000)
    public void testBroadcasterScope() {
        logger.info("Running testBroadcasterScope");
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch2 = new CountDownLatch(1);

        servletLatch = new CountDownLatch(1);
        AsyncHttpClient c = new AsyncHttpClient();
        Broadcaster b = null;
        try {
            long currentTime = System.currentTimeMillis();
            final AtomicReference<Response> r = new AtomicReference();
            c.prepareGet(urlTarget).execute(new AsyncCompletionHandler<Response>() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    r.set(response);
                    latch.countDown();
                    return response;
                }
            });

            servletLatch.await();

            String id = broadcasterId.get();

            b = BroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class, id);
            assertNotNull(b);
            b.broadcast("resume").get();

            try {
                latch.await();
            } catch (InterruptedException e) {
                fail(e.getMessage());
                return;
            }

            long time = System.currentTimeMillis() - currentTime;
            if (time < 5000) {
                assertTrue(true);
            } else {
                assertFalse(false);
            }
            assertNotNull(r.get());
            assertEquals(r.get().getStatusCode(), 200);
            String resume = r.get().getResponseBody();
            assertEquals(resume, "resumeresume");

            c.prepareGet(urlTarget).execute(new AsyncCompletionHandler<Response>() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    r.set(response);
                    latch2.countDown();
                    return response;
                }
            });

            try {
                latch2.await();
            } catch (InterruptedException e) {
                fail(e.getMessage());
                return;
            }
View Full Code Here

TOP

Related Classes of java.util.concurrent.CountDownLatch

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.