Java Component Examples

LockFreePriorityQueue Example

/*
 * Copyright (c) 2008 IBM Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.amino.examples;

/*   The Priority Queue is a queue contains elements with priority.
 *   This example show the main interfaces of LockFreePriorityQueue.
 *   Including : offer(), peek(), poll(), iterator(), size(), isEmpty()
 *  
 *   The QueueAdder class will add items of String from "1" to "ELEMENT_NUM".
 *   The QueueRemover class will remove no more than ELEMENT_NUM items.
 *   The main thread will wait for all the threads in pool to finish.  
 *   2009/05/22
 */

import java.util.AbstractQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.Iterator;

import org.amino.ds.lockfree.LockFreePriorityQueue;

public class PriorityQueueExample {

    public static final int ELEMENT_NUM = 100;
    private static final int TASKS_NUM = 4;
    
    public static void main(String[] argvs) {

        ExecutorService exec = Executors.newFixedThreadPool(TASKS_NUM);

        final AbstractQueue<String> queueStr = new LockFreePriorityQueue<String>();

        for (int i = 0; i < TASKS_NUM; ++i) {
            exec.submit(new QueueAdder(queueStr));
            if (i%2 == 0)exec.submit(new QueueRemover(queueStr));
        }

        exec.shutdown();
        
        // Wait until all the threads in pool to finish.
        try {
            while(!exec.awaitTermination(600, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    
        // If the Queue is empty now, the program will exit.
        if (queueStr.isEmpty())
        {
            System.out.println("The queue is empty, so I will exit!");
            System.exit(1);
        }

        // The poll() will delete minimum.
        String min = queueStr.poll();
        System.out.println("polled :" +min);
    
        // Print all the elements in Queue.
        for (String str : queueStr)
            System.out.println(str);
        
        System.out.println("Queue size before clearing: "+ queueStr.size());
        queueStr.clear();
        System.out.println("Queue size after clearing: "+ queueStr.size());
    }
}

/*
 *  The QueueAdder class will add elements to the given Queue.
 */
class QueueAdder implements Runnable {
    AbstractQueue<String> taskQ;
    private int count = 0;
    
    public QueueAdder(AbstractQueue<String> taskQueue)
    {
        taskQ = taskQueue;      
    }
        
    public void run(){
        while (count < PriorityQueueExample.ELEMENT_NUM)
        {
            if (taskQ.add(Integer.toString(count++)))                
                System.out.println(Thread.currentThread()+ " Offered:" + count);            
        }
        Thread.yield();
    }  
}

/*
 *  The QueueRemover class will remove the existed elements from the given Queue.
 */
class QueueRemover implements Runnable {
    AbstractQueue<String> taskQ;
    
    public QueueRemover(AbstractQueue<String> taskQueue)
    {
        taskQ = taskQueue;      
    }
        
    public void run(){
        int times = 0;
        
        while (times < PriorityQueueExample.ELEMENT_NUM)
        {
            String toRemove = taskQ.remove();
            System.out.println(Thread.currentThread()+ " Removed:" + toRemove);
            times++;
        }
        Thread.yield();
    }  
}
Back Home