Examples of Lock


Examples of java.util.concurrent.locks.Lock

    }

    public Endpoint getNextEndpoint(MessageContext synapseMessageContext,
                                    AlgorithmContext algorithmContext) {

        Lock readLock = lock.readLock();
        readLock.lock();
        try {
            if (!isThreadLocal) {
                synchronized (this) {
                    EndpointState state = endpointStates[endpointCursor];
                    if (state.getCurrentWeight() == 0) {
                        // reset the current state
                        state.reset();

                        // go to the next endpoint
                        if (endpointCursor == endpointStates.length - 1) {
                            endpointCursor = 0;
                        } else {
                            ++endpointCursor;
                        }

                        state = endpointStates[endpointCursor];
                    }

                    // we are about to use this endpoint, so decrement its current count
                    state.decrementCurrentWeight();

                    // return the endpoint corresponding to the current position
                    return endpoints.get(state.getEndpointPosition());
                }
            } else {
                if (threadedAlgorithm != null) {
                    Algorithm algo = threadedAlgorithm.get();

                    int position = algo.getNextEndpoint();

                    return endpoints.get(position);
                } else {
                    String msg = "Algorithm: WeightedRoundRobin algorithm not initialized properly";
                    log.error(msg);
                    throw new SynapseException(msg);
                }
            }
        } finally {
            readLock.unlock();
        }
    }       
View Full Code Here

Examples of java.util.concurrent.locks.Lock

            }
        });
    }

    public void changeWeight(int pos, int weight) {
        Lock writeLock = lock.writeLock();
        writeLock.lock();
        try {
            EndpointState state = null;
            for (EndpointState s : endpointStates) {
                if (s.getEndpointPosition() == pos) {
                    state = s;
                }
            }

            if (state == null) {
                throw new SynapseException("The specified endpoint position cannot be found");
            }

            state.weight = weight;

            calculate();

            reset(null);
        } finally {
            writeLock.unlock();
        }
    }
View Full Code Here

Examples of java.util.concurrent.locks.Lock

    private static void start(final int num_threads, final int num_msgs, boolean oob, int max_msg_batch_size) {
        final UNICAST unicast=new UNICAST();
        final AtomicInteger counter=new AtomicInteger(num_msgs);
        final AtomicLong seqno=new AtomicLong(1);
        final AtomicInteger delivered_msgs=new AtomicInteger(0);
        final Lock lock=new ReentrantLock();
        final Condition all_msgs_delivered=lock.newCondition();
        final ConcurrentLinkedQueue<Long> delivered_msg_list=new ConcurrentLinkedQueue<Long>();
        final Address local_addr=Util.createRandomAddress("A");
        final Address sender=Util.createRandomAddress("B");

        unicast.setDownProtocol(new Protocol() {
            public Object down(Event evt) {
                return null;
            }
        });

        unicast.setUpProtocol(new Protocol() {
            public Object up(Event evt) {
                if(evt.getType() == Event.MSG) {
                    delivered_msgs.incrementAndGet();
                    UNICAST.UnicastHeader hdr=(UNICAST.UnicastHeader)((Message)evt.getArg()).getHeader(UNICAST_ID);
                    if(hdr != null)
                        delivered_msg_list.add(hdr.getSeqno());

                    if(delivered_msgs.get() >= num_msgs) {
                        lock.lock();
                        try {
                            all_msgs_delivered.signalAll();
                        }
                        finally {
                            lock.unlock();
                        }
                    }
                }
                return null;
            }
        });

        unicast.down(new Event(Event.SET_LOCAL_ADDRESS, local_addr));
        unicast.setMaxMessageBatchSize(max_msg_batch_size);

        // send the first message manually, to initialize the AckReceiverWindow tables
        Message msg=createMessage(local_addr, sender, 1L, oob, true);
        unicast.up(new Event(Event.MSG, msg));
        Util.sleep(500);


        final CountDownLatch latch=new CountDownLatch(1);
        Adder[] adders=new Adder[num_threads];
        for(int i=0; i < adders.length; i++) {
            adders[i]=new Adder(unicast, latch, counter, seqno, oob, local_addr, sender);
            adders[i].start();
        }

        long start=System.currentTimeMillis();
        latch.countDown(); // starts all adders

        lock.lock();
        try {
            while(delivered_msgs.get() < num_msgs) {
                try {
                    all_msgs_delivered.await(1000, TimeUnit.MILLISECONDS);
                    System.out.println("received " + delivered_msgs.get() + " msgs");
                   
                    // send a spurious message to trigger removal of pending messages in AckReceiverWindow
                    msg=createMessage(local_addr, sender, 1L, oob, false);
                    unicast.up(new Event(Event.MSG, msg));
                }
                catch(InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        finally {
            lock.unlock();
        }

        long time=System.currentTimeMillis() - start;
        double requests_sec=num_msgs / (time / 1000.0);
        System.out.println("\nTime: " + time + " ms, " + Util.format(requests_sec) + " requests / sec\n");
View Full Code Here

Examples of java.util.concurrent.locks.Lock

     * - Now first node should be able to connect and first and second node should be able to merge into a group
     * - SUCCESS: a view of 2
     */
    @Test
    public void testLateStart() throws Exception {
        final Lock lock=new ReentrantLock();
        final Condition cond=lock.newCondition();
        AtomicBoolean done=new AtomicBoolean(false);

        System.out.println("-- starting first channel");
        c1=new JChannel(PROPS);
        changeMergeInterval(c1);
        c1.setReceiver(new MyReceiver("c1", done, lock, cond));
        c1.connect("demo");

        System.out.println("-- starting second channel");
        c2=new JChannel(PROPS);
        changeMergeInterval(c2);
        c2.setReceiver(new MyReceiver("c2", done, lock, cond));
        c2.connect("demo");

        System.out.println("-- starting GossipRouter");
        router=new GossipRouter(12001, bind_addr);
        router.start();

        System.out.println("-- waiting for merge to happen --");
        long target_time=System.currentTimeMillis() + 40000;
        lock.lock();
        try {
            while(System.currentTimeMillis() < target_time && done.get() == false) {
                cond.await(1000, TimeUnit.MILLISECONDS);
            }
        }
        finally {
            lock.unlock();
        }

        Util.sleep(500);
        View view=c1.getView();
        System.out.println("view=" + view);
View Full Code Here

Examples of java.util.concurrent.locks.Lock

    private static void start(final int num_threads, final int num_msgs, boolean oob, int max_msg_batch_size, TimeScheduler timer) {
        final UNICAST2 unicast=new UNICAST2();
        final AtomicInteger counter=new AtomicInteger(num_msgs);
        final AtomicLong seqno=new AtomicLong(1);
        final AtomicInteger delivered_msgs=new AtomicInteger(0);
        final Lock lock=new ReentrantLock();
        final Condition all_msgs_delivered=lock.newCondition();
        final ConcurrentLinkedQueue<Long> delivered_msg_list=new ConcurrentLinkedQueue<Long>();
        final Address local_addr=Util.createRandomAddress();
        final Address sender=Util.createRandomAddress();

        if(timer == null)
            timer=new TimeScheduler2();
        unicast.setTimer(timer);
        System.out.println("timer is a " + timer.getClass());


        unicast.setDownProtocol(new Protocol() {
            public Object down(Event evt) {return null;}
        });

        unicast.setUpProtocol(new Protocol() {
            public Object up(Event evt) {
                if(evt.getType() == Event.MSG) {
                    delivered_msgs.incrementAndGet();
                    UNICAST2.Unicast2Header hdr=(UNICAST2.Unicast2Header)((Message)evt.getArg()).getHeader(UNICAST_ID);
                    if(hdr != null)
                        delivered_msg_list.add(hdr.getSeqno());

                    if(delivered_msgs.get() >= num_msgs) {
                        lock.lock();
                        try {
                            all_msgs_delivered.signalAll();
                        }
                        finally {
                            lock.unlock();
                        }
                    }
                }
                return null;
            }
        });

        unicast.down(new Event(Event.SET_LOCAL_ADDRESS, local_addr));

        unicast.setMaxMessageBatchSize(max_msg_batch_size);
        unicast.setValue("max_bytes", 20000);

        // send the first message manually, to initialize the AckReceiverWindow tables
        Message msg=createMessage(local_addr, sender, 1L, oob, true);
        unicast.up(new Event(Event.MSG, msg));
        Util.sleep(500);


        final CountDownLatch latch=new CountDownLatch(1);
        Sender[] adders=new Sender[num_threads];
        for(int i=0; i < adders.length; i++) {
            adders[i]=new Sender(unicast, latch, counter, seqno, oob, local_addr, sender);
            adders[i].start();
        }

        long start=System.currentTimeMillis();
        latch.countDown(); // starts all adders

        lock.lock();
        try {
            while(delivered_msgs.get() < num_msgs) {
                try {
                    all_msgs_delivered.await(1000, TimeUnit.MILLISECONDS);
                    System.out.println("received " + delivered_msgs.get() + " msgs");
                }
                catch(InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        finally {
            lock.unlock();
        }

        long time=System.currentTimeMillis() - start;
        double requests_sec=num_msgs / (time / 1000.0);
        System.out.println("\nTime: " + time + " ms, " + Util.format(requests_sec) + " requests / sec\n");
View Full Code Here

Examples of java.util.concurrent.locks.Lock

    public boolean contains(T x) {
        if(x == null) {
            throw new IllegalArgumentException();
        }
        int h0 = hash0(x);
        Lock lock0 = locks[0][h0 % LOCKS];
        try {
            lock0.lock();
            if(x.equals(table[0][h0])) {
                return true;
            } else {
                int h1 = hash1(x);
                Lock lock1 = locks[1][h1 % LOCKS];
                try {
                    lock1.lock();
                    if(x.equals(table[1][h1])) {
                        return true;
                    }
                    return false;
                } finally {
                    lock1.unlock();
                }
            }
        } finally {
            lock0.unlock();
        }
View Full Code Here

Examples of java.util.concurrent.locks.Lock

    public boolean remove(T x) {
        if(x == null) {
            throw new IllegalArgumentException();
        }
        int h0 = hash0(x);
        Lock lock0 = locks[0][h0 % LOCKS];
        try {
            lock0.lock();
            if(x.equals(table[0][h0])) {
                table[0][h0] = null;
                return true;
            } else {
                int h1 = hash1(x);
                Lock lock1 = locks[1][h1 % LOCKS];
                try {
                    lock1.lock();
                    if(x.equals(table[1][h1])) {
                        table[1][h1] = null;
                        return true;
                    }
                    return false;
                } finally {
                    lock1.unlock();
                }
            }
        } finally {
            lock0.unlock();
        }
View Full Code Here

Examples of java.util.concurrent.locks.Lock

    boolean validate(TreeSet<Intent>[] intentions, List<Lock> acquired) {
        for(int phase = 0; phase < 2; phase++) {
            for(Intent intent : intentions[phase]) {
                // lock and prepare to unlock
                Lock lock = locks[phase][intent.index % LOCKS];
                lock.lock();
                acquired.add(lock);
                // beware that expected could be null
                T expected = intent.item;
                T found = table[phase][intent.index];
                if(expected == null) {
View Full Code Here

Examples of java.util.concurrent.locks.Lock

     * Synchronize before adding, removing, or testing for item
     * @param x item involved
     */
    @Override
    public final void acquire(T x) {
        Lock lock0 = lock[0][hash0(x) % lock[0].length];
        Lock lock1 = lock[1][hash1(x) % lock[1].length];
        lock0.lock();
        lock1.lock();
    }
View Full Code Here

Examples of java.util.concurrent.locks.Lock

     * synchronize after adding, removing, or testing for item
     * @param x item involved
     */
    @Override
    public final void release(T x) {
        Lock lock0 = lock[0][hash0(x) % lock[0].length];
        Lock lock1 = lock[1][hash1(x) % lock[1].length];
        lock0.unlock();
        lock1.unlock();
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.