Package java.util.concurrent

Examples of java.util.concurrent.Semaphore


     * started to do the writing, throws an exception.
     */
    private void writeLocal() throws IOException {
        PipedInputStream in = new PipedInputStream();
        final PipedOutputStream out = new PipedOutputStream(in);
        final Semaphore semaphore = new Semaphore(0);
        final Exception[] exceptions = new Exception[1];
        new Thread("RepositoryUserAdmin writer") {
            @Override
            public void run() {
                try {
                    write(out);
                }
                catch (IOException e) {
                    m_log.log(LogService.LOG_ERROR, "Error writing out contents of RepositoryAdminUser", e);
                    exceptions[0] = e;
                }
                catch (IllegalArgumentException iae) {
                    m_log.log(LogService.LOG_ERROR, "Error writing out contents of RepositoryAdminUser", iae);
                    exceptions[0] = iae;
                }
                semaphore.release();
            }
        }.start();
        m_repository.writeLocal(in);
        try {
            if (!semaphore.tryAcquire(30, TimeUnit.SECONDS)) {
                throw new IOException("Error writing the contents of RepositoryUserAdmin.");
            }
        }
        catch (InterruptedException e) {
            Thread.currentThread().interrupt();
View Full Code Here


    }

    private static void resetStatus() {
      runStarted.release(Integer.MAX_VALUE);
      finishRun.release(Integer.MAX_VALUE);
      runStarted = new Semaphore(0);
      finishRun = new Semaphore(0);
      timesRun.set(0);
    }
View Full Code Here

        private Semaphore semaphore;

        public UserSimulator(LocalAvatar avatar) {
            super("UserSimulator");
            this.avatar = avatar;
            semaphore = new Semaphore(0);
        }
View Full Code Here

                              final String fragmentFile,
                              final ShaderVariableBinder binder)
    {
        // the sceneworker will set this value
        final MutableBoolean out = new MutableBoolean();
        final Semaphore done = new Semaphore(0);

        final String vertex = readShader(vertexFile);
         final String fragment = readShader(fragmentFile);

        SceneWorker.addWorker(new WorkCommit() {
            public void commit() {
                try {
                    RenderManager rm = ClientContextJME.getWorldManager().getRenderManager();
                    GLSLShaderObjectsState shaderState = (GLSLShaderObjectsState)rm.createRendererState(StateType.GLSLShaderObjects);
                    shaderState.setEnabled(true);
                    shaderState.load(vertex, fragment);

                    if (binder != null) {
                        binder.bind(shaderState);
                    }

                    shaderState.apply();
                } catch (GLException ex) {
                    logger.log(Level.WARNING, "Unable to load avatar sample " +
                               "shader. High quality avatars are not available.",
                               ex);
                    out.value = false;
                } finally {
                    done.release();
                }
            }
        });

        try {
            done.acquire();
            return out.value;
        } catch (InterruptedException ie) {
            return false;
        }
    }
View Full Code Here

     *                   Must be >0 and < {@link Integer#MAX_VALUE}.
     * @param fair <tt>true</tt> guarantees the waiting schedulers to wake up in order they acquired a permit
     */
    ThreadResourcesBalancer( int numPermits, boolean fair )
    {
        balancer = new Semaphore( numPermits, fair );
        this.numPermits = numPermits;
    }
View Full Code Here

         final int totalrepeats = 3;

         final AtomicInteger errors = new AtomicInteger(0);

         // We shouldn't have more than 10K messages pending
         final Semaphore semop = new Semaphore(10000);

         class ConsumerThread extends Thread
         {
            public void run()
            {
               try
               {
                  ServerLocator locator = HornetQClient.createServerLocatorWithoutHA(server1tc);

                  ClientSessionFactory sf = locator.createSessionFactory();

                  ClientSession session = sf.createSession(false, false);

                  session.start();

                  ClientConsumer consumer = session.createConsumer(queueName1);

                  for (int i = 0; i < numMessages; i++)
                  {
                     ClientMessage message = consumer.receive(5000);

                     Assert.assertNotNull(message);

                     message.acknowledge();
                     semop.release();
                     if (i % 1000 == 0)
                     {
                        session.commit();
                     }
                  }

                  session.commit();

                  session.close();
                  sf.close();
                  locator.close();

               }
               catch (Throwable e)
               {
                  e.printStackTrace();
                  errors.incrementAndGet();
               }
            }
         };

         class ProducerThread extends Thread
         {
            final int nmsg;
            ProducerThread(int nmsg)
            {
               this.nmsg = nmsg;
            }
            public void run()
            {
               ServerLocator locator = HornetQClient.createServerLocatorWithoutHA(server0tc);
              
               locator.setBlockOnDurableSend(false);
               locator.setBlockOnNonDurableSend(false);

               ClientSessionFactory sf = null;

               ClientSession session = null;

               ClientProducer producer = null;

               try
               {
                  sf = locator.createSessionFactory();

                  session = sf.createSession(false, true, true);

                  producer = session.createProducer(new SimpleString(testAddress));

                  for (int i = 0; i < nmsg; i++)
                  {
                     assertEquals(0, errors.get());
                     ClientMessage message = session.createMessage(true);

                     message.putIntProperty("seq", i);
                    
                    
                     if (i % 100 == 0)
                     {
                        message.setPriority((byte)(RandomUtil.randomPositiveInt() % 9));
                     }
                     else
                     {
                        message.setPriority((byte)5);
                     }

                     message.getBodyBuffer().writeBytes(new byte[50]);

                     producer.send(message);
                     assertTrue(semop.tryAcquire(1, 10, TimeUnit.SECONDS));
                  }
               }
               catch (Throwable e)
               {
                  e.printStackTrace(System.out);
View Full Code Here

        public URL url = null;

        private Job(JobType jobType, ImiAvatar avatar) {
            this.jobType = jobType;
            this.avatar = avatar;
            this.jobDone = new Semaphore(0);
            this.url = null;
        }
View Full Code Here

        private final URI uri;

        private Pool(final URI uri, final int size, final long timeout) {
            this.uri = uri;
            this.size = size;
            this.semaphore = new Semaphore(size);
            this.pool = new Stack<SocketConnection>();
            this.timeout = timeout;
            this.timeUnit = TimeUnit.MILLISECONDS;

            for (int i = 0; i < size; i++) {
View Full Code Here

    public static class Parallel {

        public static void exec(Runnable... runnables) {
            final int permits = 2;
            final Semaphore semaphore = new Semaphore(permits);

            for (Runnable runnable : runnables) {
                try {
                    semaphore.acquire();
                    final Thread thread = new Thread();
                    thread.setDaemon(true);
                    thread.start();
                } catch (InterruptedException e) {
                    Thread.interrupted();
                }
            }
            try {
                semaphore.acquire(permits);
            } catch (InterruptedException e) {
                Thread.interrupted();
            }
        }
View Full Code Here

        private Semaphore semaphore;
       
        public Data(int poolLimit, boolean strictPooling) {
            pool = new LinkedListStack(poolLimit);
            if (strictPooling) {
                semaphore = new Semaphore(poolLimit);
            }
        }
View Full Code Here

TOP

Related Classes of java.util.concurrent.Semaphore

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.