Java Component Examples

LockFreeVector 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;

/*   This example shows the mainly interfaces of LockFreeVector.
 *   Including : pushBack(), popBack(), get(), set().
 *  
 *   The VectorAdder class will add different items of String from "1" to "ELEMENT_NUM".
 *   The VectorRemover 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.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.amino.ds.lockfree.LockFreeVector;

public class VectorExample {
    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 LockFreeVector<String> vectorStr = new LockFreeVector<String>();

        for (int i = 0; i < TASKS_NUM; ++i) {
            exec.submit(new VectorAdder(vectorStr));
            if (i%2 == 0)exec.submit(new VectorRemover(vectorStr));
        }

        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 Vector is empty now, the program will exit.
        if (vectorStr.isEmpty())
        {
            System.out.println("The Vector is empty, so I will exit!");
            System.exit(1);
        }
        
        // Print all the elements in List.
        for (String str : vectorStr)
            System.out.println(str);
        
        // Let's try get() and set() here.
        String str = vectorStr.get(0);
        if ( null == str)
            vectorStr.set(0, "0");
        
        System.out.println("The Vector size now: " + vectorStr.size());
    }
}
/*
 *  The VectorAdder class will add elements to the given Vector.
 */
class VectorAdder implements Runnable {
    LockFreeVector<String> taskVec;
    private int count = 0;
    
    public VectorAdder(LockFreeVector<String> taskVector)
    {
        taskVec = taskVector;      
    }
        
    public void run(){
        while (count < VectorExample.ELEMENT_NUM)
        {
            taskVec.pushBack(Integer.toString(count++));
            System.out.println(Thread.currentThread()+ " Pushed: " + count);
        }
        Thread.yield();
    }  
}

/*
 *  The VectorRemover class will remove the existed elements from the given Vector.
 */
class VectorRemover implements Runnable {
    LockFreeVector<String> taskVec;
    
    public VectorRemover(LockFreeVector<String> taskVector)
    {
        taskVec = taskVector;      
    }
        
    public void run(){
        int times = 0;
        
        while (times < VectorExample.ELEMENT_NUM)
        {
            String popped = taskVec.popBack();
            System.out.println(Thread.currentThread()+ "Popped: " + popped);
            times++;
        }
        Thread.yield();
    }  
}
Back Home