Package org.jgroups.util

Examples of org.jgroups.util.Queue


    }


    public static void testRemoveElementThreeElementsThirdFound() {
        String s1="Q1", s2="Q2", s3="Q3";
        final Queue queue=new Queue();

        try {
            queue.add(s1);
            queue.add(s2);
            queue.add(s3);
            queue.removeElement(s3);
            assert queue.size() == 2;
            assert queue.getFirst().equals(s1);
            assert queue.getLast().equals(s2);
        }
        catch(QueueClosedException ex) {
            assert false : ex.toString();
        }
    }
View Full Code Here


    }


    @Test(expectedExceptions=QueueClosedException.class)
    public static void testRemoveAndClose() throws QueueClosedException {
        final Queue queue=new Queue();
        new Thread() {
            public void run() {
                Util.sleep(1000);
                queue.close(true); // close gracefully
            }
        }.start();
        queue.remove();
    }
View Full Code Here

    }


    @Test(expectedExceptions=QueueClosedException.class)
    public static void testRemoveAndCloseWithTimeout() throws QueueClosedException, TimeoutException {
        final Queue queue=new Queue();
        new Thread() {
            public void run() {
                Util.sleep(1000);
                queue.close(true); // close gracefully
            }
        }.start();

        queue.remove(5000);
    }
View Full Code Here

    }


    @Test(expectedExceptions=TimeoutException.class)
    public static void testInterruptAndRemove() throws QueueClosedException, TimeoutException {
        final Queue queue=new Queue();
        Thread.currentThread().interrupt();
        queue.remove(2000);
    }
View Full Code Here

    }


    @Test(expectedExceptions=QueueClosedException.class)
    public static void testRemoveAndInterrupt() throws QueueClosedException {
        final Queue queue=new Queue();

        Thread closer=new Thread() {
            public void run() {
                Util.sleep(1000);
                System.out.println("-- closing queue");
                queue.close(false);
            }
        };
        closer.start();

        System.out.println("-- removing element");
        queue.remove();
    }
View Full Code Here

        queue.remove();
    }


    public static void testClear() throws QueueClosedException {
        Queue queue=new Queue();
        queue.add("one");
        queue.add("two");
        assert queue.size() == 2;
        queue.close(true);
        assert queue.size() == 2;
        queue.clear();
        assert queue.size() == 0;
        queue=new Queue();
        queue.add("one");
        queue.add("two");
        queue.clear();
        assert queue.size() == 0;
        queue.add("one");
        queue.add("two");
        assert queue.size() == 2;
        queue.clear();
        assert queue.size() == 0;
    }
View Full Code Here

    /** Multiple threads call remove(), one threads then adds an element. Only 1 thread should actually terminate
     * (the one that has the element) */
    public static void testBarrier() throws QueueClosedException {
        RemoveOneItem[] removers=new RemoveOneItem[10];
        final Queue queue=new Queue();
        int num_dead=0;

        for(int i=0; i < removers.length; i++) {
            removers[i]=new RemoveOneItem(i, queue);
            removers[i].start();
        }

        Util.sleep(200);

        System.out.println("-- adding element 99");
        queue.add(new Long(99));
        System.out.println("-- adding element 100");
        queue.add(new Long(100));

        long target_time=System.currentTimeMillis() + 10000L;
        do {
            int num=0;
            for(int i=0; i < removers.length; i++) {
                if(!removers[i].isAlive())
                    num++;
            }
            if(num == 2)
                break;
            Util.sleep(500);
        }
        while(target_time > System.currentTimeMillis());


        for(int i=0; i < removers.length; i++) {
            System.out.println("remover #" + i + " is " + (removers[i].isAlive() ? "alive" : "terminated"));
            if(!removers[i].isAlive()) {
                num_dead++;
            }
        }

        assert num_dead == 2 : "num_dead was " + num_dead + ", but expected 2";
        queue.close(false);
    }
View Full Code Here

    }

    /** Multiple threads call remove(), one threads then adds an element. Only 1 thread should actually terminate
     * (the one that has the element) */
    public static void testBarrierWithTimeOut() throws QueueClosedException {
        final Queue queue=new Queue();
        RemoveOneItemWithTimeout[] removers=new RemoveOneItemWithTimeout[10];
        int num_dead=0;

        for(int i=0; i < removers.length; i++) {
            removers[i]=new RemoveOneItemWithTimeout(i, 15000, queue);
            removers[i].start();
        }

        System.out.println("-- adding element 99");
        queue.add(new Long(99));
        System.out.println("-- adding element 100");
        queue.add(new Long(100));

        long target_time=System.currentTimeMillis() + 10000L;
        do {
            int num_rsps=0;
            for(int i=0; i < removers.length; i++) {
                if(removers[i].getRetval() != null)
                    num_rsps++;
            }
            if(num_rsps == 2)
                break;
            Util.sleep(500);
        }
        while(target_time > System.currentTimeMillis());

        Util.sleep(3000);

        for(int i=0; i < removers.length; i++) {
            System.out.println("remover #" + i + " is " + (removers[i].isAlive() ? "alive" : "terminated"));
            if(!removers[i].isAlive()) {
                num_dead++;
            }
        }

        assert num_dead == 2 : "num_dead should have been 2 but was " + num_dead;

        System.out.println("closing queue - causing all remaining threads to terminate");
        queue.close(false); // will cause all threads still blocking on remove() to return
        Util.sleep(500);

        num_dead=0;
        for(int i=0; i < removers.length; i++) {
            System.out.println("remover #" + i + " is " + (removers[i].isAlive()? "alive" : "terminated"));
View Full Code Here

    /** Multiple threads add one element, one thread read them all.
     * (the one that has the element) */

    public static void testMultipleWriterOneReader() throws QueueClosedException {
        final Queue queue=new Queue();
        AddOneItem[] adders=new AddOneItem[10];
        int num_dead=0;
        int num_items=0;
        int items=1000;

        for(int i=0; i < adders.length; i++) {
            adders[i]=new AddOneItem(i, items, queue);
            adders[i].start();
        }

        Util.sleep(500);
        while(num_items < (adders.length * items)) {
            queue.remove();
            num_items++;
        }

        Util.sleep(1000);

        for(int i=0; i < adders.length; i++) {
            System.out.println("adder #" + i + " is " + (adders[i].isAlive()? "alive" : "terminated"));
            if(!adders[i].isAlive()) {
                num_dead++;
            }
        }

        assert num_dead == 10 : "num_dead should have been 10 but was " + num_dead;
        queue.close(false); // will cause all threads still blocking on peek() to return
    }
View Full Code Here

    /**
     * Times how long it takes to add and remove 1000000 elements concurrently (1 reader, 1 writer)
     */

    public static void testConcurrentAddRemove() throws QueueClosedException {
        final Queue queue=new Queue();
        final long   NUM=1000000;
        long         num_received=0;
        Object       ret;
        long         start, stop;

        start=System.currentTimeMillis();

        new Thread() {
            public void run() {
                for(int i=0; i < NUM; i++) {
                    try {
                        queue.add(new Object());
                    }
                    catch(QueueClosedException e) {
                    }
                }
            }
        }.start();

        while(num_received < NUM) {
            ret=queue.remove();
            if(ret != null)
                num_received++;
        }
        assert num_received == NUM;
        stop=System.currentTimeMillis();
View Full Code Here

TOP

Related Classes of org.jgroups.util.Queue

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.