Java Q&A
by Peter Haggar

Listing One
class RealTimeClock 
{
   private int clkID;
   public int clockID()
   {
      return clkID;
   }
   public void setClockID(int id)
   {
      clkID =id;
   }
   //...
}


Listing Two
public class AtomicLong extends Thread 
{
   static volatile long val;
   static int count =10000000;
   long key;
   AtomicLong(int k)
   {
      long temp =k;
      key =(temp<<32)|temp;   //key =00000001 00000001 for thread 1
   }                          //key =00000002 00000002 for thread 2 etc
   public void run()
   {
      for(int i =0;i <count;i++)
      {
         //This 64 bit assignment is supposed to be atomic since val is declared   
         //atomic
         long temp =val;//temp =00000001 00000001
         long temp1 =temp>>>32;//temp1 =00000000 00000001
         long temp2 =temp<<32;//temp2 =00000001 00000000
         temp2 =temp2>>>32;//temp2 =00000000 00000001

         if (temp1 !=temp2)
         {
            System.out.println("Saw:"+Long.toHexString(temp));
            System.out.println("temp1 is:"+Long.toHexString(temp1));
            System.out.println("temp2 is:"+Long.toHexString(temp2));
            System.exit(1);
         }
         //This 64-bit assignment is supposed to be atomic since val is
         //declared volatile.temp1 should always equal temp2.
         val =key;//val should always =00000001 00000001 for thread 1
     }
  }
  public static void main(String args [] )
  {
     for(int t =1;t <10;t++)
     new AtomicLong(t).start();
   }
}





1

