Home  >  Core Java

Example of AtomicStampedReference in Java

By Arvind Rai, December 26, 2012
1. AtomicStampedReference belongs to the package java.util.concurrent.atomic. It is available in jdk1.5.
2. java.util.concurrent.atomic keeps a reference and a stamp is associated with it.
3. While creating object of AtomicStampedReference , an initial reference and initial stamp is passed.
4. Initial reference and initial stamp can be updated atomitically.
package com.concretepage;

import java.util.concurrent.atomic.AtomicStampedReference;

public class AtomicStampedReferenceTest {
	
	AtomicStampedReference<String> asr= new AtomicStampedReference<String>("ref",10);
	
	class Worker implements Runnable{

		@Override
		public void run() {
			
		//if current reference is "ref" then initial stamp is chnaged to 20 
		asr.attemptStamp("ref",20);
		
		//if current value is ref, then it is set as newref
		asr.weakCompareAndSet("ref","newref",20,30);
	 }
		
	}
	
   public static void main(String... args){
	   new Thread(new AtomicStampedReferenceTest().new Worker()).start();
	   new Thread(new AtomicStampedReferenceTest().new Worker()).start();
   }
}
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us