All Downloads are FREE. Search and download functionalities are using the official Maven repository.

blah.concurrency.second.Cursor Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package blah.concurrency.second;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class Cursor {

	final FieldReference cursorField = FieldReference.wrap( this, "cursor" );
	final int threshold;
	volatile int cursor;

	public int next() {
		return next( 1 );
	}

	public int next( int increment ){
		int current, next;
		while( true ) {
			current = current();
			next = (current + increment) % threshold;
			if ( cursorField.compareAndSet(current, next) )
				return next;
		}
	}

	public int current(){
		return cursor;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy