Changeset 499 for Tests


Ignore:
Timestamp:
03/26/12 19:28:32 (10 years ago)
Author:
BegemoT
Message:
 
Location:
Tests/JAVA/test/src/main/java/test/threads/queue/unstressed/impl
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • Tests/JAVA/test/src/main/java/test/threads/queue/unstressed/impl/ABQBusyWaitQueue.java

    r492 r499  
    7979    private void backoff( final int turn ) throws InterruptedException { 
    8080        Thread.yield(); 
     81 
     82        /*if ( turn % 10 == 9 ) { 
     83            Thread.yield(); 
     84        } else { 
     85            for ( int i = 0; i < turn; i++ ) { 
     86                if ( Thread.currentThread().isInterrupted() ) { 
     87                    return; 
     88                } 
     89            } 
     90        }*/ 
     91 
    8192        /*final int mod = turn & 0xf; 
    8293        if ( mod != 0 ) { 
  • Tests/JAVA/test/src/main/java/test/threads/queue/unstressed/impl/ABQConditionFreeQueue.java

    r492 r499  
    11package test.threads.queue.unstressed.impl; 
    22 
    3 import java.util.concurrent.atomic.AtomicInteger; 
     3import java.util.concurrent.locks.ReentrantLock; 
    44 
    55import test.threads.queue.unstressed.IBoundedQueueFactory; 
     
    77 
    88/** 
    9  * ABQ with busy-wait instead of parking 
     9 * ABQ with busy-wait instead of condition-based notification 
    1010 * 
    1111 * @author cheremin 
    1212 * @since 24.03.12,  1:57 
    1313 */ 
    14 public class ABQBusyWaitQueue<T> implements IQueue<T> { 
    15     private static final boolean USE_TEST_AND_CAS = false; 
     14public class ABQConditionFreeQueue<T> implements IQueue<T> { 
    1615 
    1716    private static final int FREE = 0; 
     
    2625    private int count = 0; 
    2726 
    28     private final AtomicInteger owned = new AtomicInteger( FREE ); 
     27    private final ReentrantLock lock = new ReentrantLock(); 
    2928 
    3029    @SuppressWarnings( "unchecked" ) 
    31     public ABQBusyWaitQueue( final int size ) { 
     30    public ABQConditionFreeQueue( final int size ) { 
    3231        this.items = ( T[] ) new Object[size]; 
    3332    } 
     
    7978    private void backoff( final int turn ) throws InterruptedException { 
    8079        Thread.yield(); 
     80 
     81        /*if ( turn % 10 == 9 ) { 
     82            Thread.yield(); 
     83        } else { 
     84            for ( int i = 0; i < turn; i++ ) { 
     85                if ( Thread.currentThread().isInterrupted() ) { 
     86                    return; 
     87                } 
     88            } 
     89        }*/ 
     90 
    8191        /*final int mod = turn & 0xf; 
    8292        if ( mod != 0 ) { 
     
    92102 
    93103    private void releaseLock() { 
    94         owned.set( FREE ); 
     104        lock.unlock(); 
    95105    } 
    96106 
    97107    private boolean tryAcquireLock() throws InterruptedException { 
    98         if ( USE_TEST_AND_CAS && owned.get() != FREE ) { 
    99             return false; 
    100         } 
    101         return owned.compareAndSet( FREE, OWNED ); 
     108        return lock.tryLock(); 
    102109    } 
    103110 
     
    109116 
    110117    @SuppressWarnings( "unchecked" ) 
    111     public static <T> IBoundedQueueFactory<T, ABQBusyWaitQueue<T>> factory() { 
     118    public static <T> IBoundedQueueFactory<T, ABQConditionFreeQueue<T>> factory() { 
    112119        return FACTORY; 
    113120    } 
    114121 
    115     public static final IBoundedQueueFactory FACTORY = new IBoundedQueueFactory<Object, ABQBusyWaitQueue<Object>>() { 
     122    public static final IBoundedQueueFactory FACTORY = new IBoundedQueueFactory<Object, ABQConditionFreeQueue<Object>>() { 
    116123        @Override 
    117         public ABQBusyWaitQueue<Object> create( final int size ) { 
    118             return new ABQBusyWaitQueue<Object>( size ); 
     124        public ABQConditionFreeQueue<Object> create( final int size ) { 
     125            return new ABQConditionFreeQueue<Object>( size ); 
    119126        } 
    120127 
    121128        @Override 
    122129        public String toString() { 
    123             return "ABQBusyWaitFactory[" + ( USE_TEST_AND_CAS ? "TCAS" : "CAS" ) + "]"; 
     130            return "ABQConditionFreeFactory"; 
    124131        } 
    125132    }; 
Note: See TracChangeset for help on using the changeset viewer.