Package EDU.oswego.cs.dl.util.concurrent

Examples of EDU.oswego.cs.dl.util.concurrent.Semaphore


   
    /**
     */
    private void runConcurrentTest(int workers, final Callable test) throws InterruptedException, Throwable {
        final Throwable workerError[] = new Throwable[1];
        final Semaphore doneSemaphore = new Semaphore(1-workers);
        for( int i=0; i < workers; i++ ) {
          final String name = ""+i;
            new Thread() {
                public void run() {
                    try {
                      Thread.currentThread().setName(name);
                        test.call();
                    } catch (Throwable e) {
                        workerError[0] = e;
                    } finally {
                        doneSemaphore.release();
                    }
                }
            }.start();
        }
        doneSemaphore.acquire();
        if( workerError[0] != null )
            throw workerError[0];
    }
View Full Code Here


        final int PRODUCER_COUNT=Integer.parseInt(System.getProperty("PRODUCER_COUNT","10"));
        final int CONSUMER_COUNT=Integer.parseInt(System.getProperty("CONSUMER_COUNT","10"));
       
        final ProgressPrinter pp = new ProgressPrinter(MESSAGE_COUNT*2, 5);

        final Semaphore connectionsEstablished = new Semaphore(1-(CONSUMER_COUNT+PRODUCER_COUNT));
        final Latch startTest = new Latch();
        final Semaphore testsFinished = new Semaphore(1-(CONSUMER_COUNT+PRODUCER_COUNT));
       
        final Callable producer = new Callable() {
            public Object call() throws JMSException, InterruptedException {
                Connection connection = connectionFactory.createConnection();
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer producer = session.createProducer(dest);
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                BytesMessage message = session.createBytesMessage();
                message.writeBytes(new byte[1024]);
                connection.start();
               
                connectionsEstablished.release();
               
                startTest.acquire();               
                final int msgs = (MESSAGE_COUNT/PRODUCER_COUNT)+1;
                for (int i = 0; i < msgs; i++) {
                    pp.increment();
                    producer.send(message);
                }               
               
                testsFinished.release();               
                connection.close();
                return null;
            }
        };
       
        final Callable consumer = new Callable() {
            public Object call() throws JMSException, InterruptedException {
                final Latch doneLatch = new Latch();               
                Connection connection = connectionFactory.createConnection();
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageConsumer consumer = session.createConsumer(dest);

                connectionsEstablished.release();
                startTest.acquire();

                final int msgs = (MESSAGE_COUNT/CONSUMER_COUNT)-1;
                consumer.setMessageListener(new MessageListener(){
                    int counter=0;
                    public void onMessage(Message msg) {               
                        pp.increment();
                        counter++;
                        if( counter >= msgs ) {
                            doneLatch.release();
                        }
                    }
                });
                connection.start();
                doneLatch.acquire();

                testsFinished.release();               
                connection.close();
                return null;
            }
        };
       
        final Throwable workerError[] = new Throwable[1];
        for( int i=0; i < PRODUCER_COUNT; i++ ) {
            new Thread("Producer:"+i) {
                public void run() {
                    try {
                        producer.call();
                    } catch (Throwable e) {
                        e.printStackTrace();
                        workerError[0] = e;
                    }
                }
            }.start();
        }

        for( int i=0; i < CONSUMER_COUNT; i++ ) {
            new Thread("Consumer:"+i) {
                public void run() {
                    try {
                        consumer.call();
                    } catch (Throwable e) {
                        workerError[0] = e;
                    }
                }
            }.start();
        }

        connectionsEstablished.acquire();

//        System.out.println("ready.");
//        System.in.read();System.in.read();
       
        startTest.release();
        long start = System.currentTimeMillis();
        testsFinished.acquire();
        long end = System.currentTimeMillis();
        System.out.println(getName() + ": test duration: " + (end - start) + " ms, published+acked msg/s: "
                + (MESSAGE_COUNT * 1000f / (end - start)));
       
//        System.out.println("ready.");
View Full Code Here

   
    /**
     */
    private void runConcurrentTest(int workers, final Callable test) throws InterruptedException, Throwable {
        final Throwable workerError[] = new Throwable[1];
        final Semaphore doneSemaphore = new Semaphore(1-workers);
        for( int i=0; i < workers; i++ ) {
          final String name = ""+i;
            new Thread() {
                public void run() {
                    try {
                      Thread.currentThread().setName(name);
                        test.call();
                    } catch (Throwable e) {
                        workerError[0] = e;
                    } finally {
                        doneSemaphore.release();
                    }
                }
            }.start();
        }
        doneSemaphore.acquire();
        if( workerError[0] != null )
            throw workerError[0];
    }
View Full Code Here

        final int PRODUCER_COUNT=Integer.parseInt(System.getProperty("PRODUCER_COUNT","10"));
        final int CONSUMER_COUNT=Integer.parseInt(System.getProperty("CONSUMER_COUNT","10"));
       
        final ProgressPrinter pp = new ProgressPrinter(MESSAGE_COUNT*2, 5);

        final Semaphore connectionsEstablished = new Semaphore(1-(CONSUMER_COUNT+PRODUCER_COUNT));
        final Latch startTest = new Latch();
        final Semaphore testsFinished = new Semaphore(1-(CONSUMER_COUNT+PRODUCER_COUNT));
       
        final Callable producer = new Callable() {
            public Object call() throws JMSException, InterruptedException {
                Connection connection = connectionFactory.createConnection();
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer producer = session.createProducer(dest);
                producer.setDeliveryMode(DeliveryMode.PERSISTENT);
                BytesMessage message = session.createBytesMessage();
                message.writeBytes(new byte[1024]);
                connection.start();
               
                connectionsEstablished.release();
               
                startTest.acquire();               
                final int msgs = (MESSAGE_COUNT/PRODUCER_COUNT)+1;
                for (int i = 0; i < msgs; i++) {
                    pp.increment();
                    producer.send(message);
                }               
               
                testsFinished.release();               
                connection.close();
                return null;
            }
        };
       
        final Callable consumer = new Callable() {
            public Object call() throws JMSException, InterruptedException {
                final Latch doneLatch = new Latch();               
                Connection connection = connectionFactory.createConnection();
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageConsumer consumer = session.createConsumer(dest);

                connectionsEstablished.release();
                startTest.acquire();

                final int msgs = (MESSAGE_COUNT/CONSUMER_COUNT)-1;
                consumer.setMessageListener(new MessageListener(){
                    int counter=0;
                    public void onMessage(Message msg) {               
                        pp.increment();
                        counter++;
                        if( counter >= msgs ) {
                            doneLatch.release();
                        }
                    }
                });
                connection.start();
                doneLatch.acquire();

                testsFinished.release();               
                connection.close();
                return null;
            }
        };
       
        final Throwable workerError[] = new Throwable[1];
        for( int i=0; i < PRODUCER_COUNT; i++ ) {
            new Thread("Producer:"+i) {
                public void run() {
                    try {
                        producer.call();
                    } catch (Throwable e) {
                        e.printStackTrace();
                        workerError[0] = e;
                    }
                }
            }.start();
        }

        for( int i=0; i < CONSUMER_COUNT; i++ ) {
            new Thread("Consumer:"+i) {
                public void run() {
                    try {
                        consumer.call();
                    } catch (Throwable e) {
                        workerError[0] = e;
                    }
                }
            }.start();
        }

        connectionsEstablished.acquire();

//        System.out.println("ready.");
//        System.in.read();System.in.read();
       
        startTest.release();
        long start = System.currentTimeMillis();
        testsFinished.acquire();
        long end = System.currentTimeMillis();
        System.out.println(getName() + ": test duration: " + (end - start) + " ms, published+acked msg/s: "
                + (MESSAGE_COUNT * 1000f / (end - start)));
       
//        System.out.println("ready.");
View Full Code Here

         if (pool == null)
         {
            pool = new LinkedList();
            connectionPools.put(address, pool);
            log.debug("Creating semaphore with size " + maxPoolSize);
            semaphore = new Semaphore(maxPoolSize);
            semaphores.put(address, semaphore);
           
            if (trace)
            {
               synchronized (pool)
View Full Code Here

TOP

Related Classes of EDU.oswego.cs.dl.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.