- Timestamp:
- 03/26/12 19:28:32 (10 years ago)
- 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 79 79 private void backoff( final int turn ) throws InterruptedException { 80 80 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 81 92 /*final int mod = turn & 0xf; 82 93 if ( mod != 0 ) { -
Tests/JAVA/test/src/main/java/test/threads/queue/unstressed/impl/ABQConditionFreeQueue.java
r492 r499 1 1 package test.threads.queue.unstressed.impl; 2 2 3 import java.util.concurrent. atomic.AtomicInteger;3 import java.util.concurrent.locks.ReentrantLock; 4 4 5 5 import test.threads.queue.unstressed.IBoundedQueueFactory; … … 7 7 8 8 /** 9 * ABQ with busy-wait instead of parking9 * ABQ with busy-wait instead of condition-based notification 10 10 * 11 11 * @author cheremin 12 12 * @since 24.03.12, 1:57 13 13 */ 14 public class ABQBusyWaitQueue<T> implements IQueue<T> { 15 private static final boolean USE_TEST_AND_CAS = false; 14 public class ABQConditionFreeQueue<T> implements IQueue<T> { 16 15 17 16 private static final int FREE = 0; … … 26 25 private int count = 0; 27 26 28 private final AtomicInteger owned = new AtomicInteger( FREE);27 private final ReentrantLock lock = new ReentrantLock(); 29 28 30 29 @SuppressWarnings( "unchecked" ) 31 public ABQ BusyWaitQueue( final int size ) {30 public ABQConditionFreeQueue( final int size ) { 32 31 this.items = ( T[] ) new Object[size]; 33 32 } … … 79 78 private void backoff( final int turn ) throws InterruptedException { 80 79 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 81 91 /*final int mod = turn & 0xf; 82 92 if ( mod != 0 ) { … … 92 102 93 103 private void releaseLock() { 94 owned.set( FREE);104 lock.unlock(); 95 105 } 96 106 97 107 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(); 102 109 } 103 110 … … 109 116 110 117 @SuppressWarnings( "unchecked" ) 111 public static <T> IBoundedQueueFactory<T, ABQ BusyWaitQueue<T>> factory() {118 public static <T> IBoundedQueueFactory<T, ABQConditionFreeQueue<T>> factory() { 112 119 return FACTORY; 113 120 } 114 121 115 public static final IBoundedQueueFactory FACTORY = new IBoundedQueueFactory<Object, ABQ BusyWaitQueue<Object>>() {122 public static final IBoundedQueueFactory FACTORY = new IBoundedQueueFactory<Object, ABQConditionFreeQueue<Object>>() { 116 123 @Override 117 public ABQ BusyWaitQueue<Object> create( final int size ) {118 return new ABQ BusyWaitQueue<Object>( size );124 public ABQConditionFreeQueue<Object> create( final int size ) { 125 return new ABQConditionFreeQueue<Object>( size ); 119 126 } 120 127 121 128 @Override 122 129 public String toString() { 123 return "ABQ BusyWaitFactory[" + ( USE_TEST_AND_CAS ? "TCAS" : "CAS" ) + "]";130 return "ABQConditionFreeFactory"; 124 131 } 125 132 };
Note: See TracChangeset
for help on using the changeset viewer.