Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5056417

java.util.concurrent.atomic.AtomicIntegerArray: concurrent snapshot of array.

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs



      Name: rmT116609 Date: 06/02/2004


      A DESCRIPTION OF THE REQUEST :
      AtomicIntegerArray provides atomic transaction support for Thread safe access to Integer arrays. However it is impossible to obtain an atomic concurrent snapshot of the contents of the array. A programmer has the ability to obtain the value at a specific index, but not the entire array.

      Secondly; the ability to add content from another array Similar to System.arraycopy(....) but to provide atomicity and concurrency....

      JUSTIFICATION :
      At present the class provides atomic support for all ints in the array but not on the array as a whole
      For example to retreive the content of the entire array
      assuming AtomicIntegerArray aia;
      int s = aia.length();
      int[] i = new int[s];
      for(int j=0;j<s;j++){
      i[j] = aia.get(j);
      }
      But this is not thread safe and may result in an element being modified by another Thread during this process..... A workaround would involve synchronizing on the AtomicIntegerArray



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      int[] i = aia.toArray();

      ---------- BEGIN SOURCE ----------
      import java.util.*;
      import java.util.concurrent.atomic.*;

      public class AtomicArrayTest
      {
           public AtomicIntegerArray aia1 = new AtomicIntegerArray(6);
           

           public static void main(String args[])throws InterruptedException //make 2 equal threads
           {
                if(args.length != 1)
                {
                     System.out.println("Usage: java AtomicArayTest <seconds>");
                     System.exit(1);
                }
                int loop = (Integer.parseInt(args[0])*1000);
                long start = System.currentTimeMillis();
                AtomicArrayTest s = new AtomicArrayTest();
                modThread m1 = new modThread(s, 1);
                modThread m2 = new modThread(s, 2);
                m1.start();
                m2.start();
                
                while( (System.currentTimeMillis() - start) < loop)
                {
                     Thread.sleep(10000);
                }
                m1.exit();
                m2.exit();
           }

           public static class modThread extends Thread
           {
                int reads = 0;
                int writes = 0;
                int cerror = 0;
                public volatile boolean running = true;
                int m;
                AtomicArrayTest s;
                Random rand = new Random();
                
                public modThread(AtomicArrayTest ss, int id)
                {
                     s = ss;
                     m = id;
                     this.setPriority(Thread.MAX_PRIORITY); // higher interrupt power
                }
                
                public synchronized void exit()
                {
                     running=false;
                     System.out.println(m+": c errors "+cerror+", reads "+reads+", writes "+writes);
                }
                
                public void run()
                {
                     while(running)
                     {
                          if(rand.nextInt(50) < 25) //get a byte array
                          {
                               if( (s.aia1.get(0) != s.aia1.get(1)) ||
                                   (s.aia1.get(1) != s.aia1.get(2)) ||
                                   (s.aia1.get(2) != s.aia1.get(3)) ||
                                   (s.aia1.get(3) != s.aia1.get(4)) ||
                                   (s.aia1.get(4) != s.aia1.get(5)) )
                               {
                                    
                                    cerror++;
                               }
                               reads++;
                          }
                          else //set the contents equal
                          {
                               try
                               {
                                    int p = rand.nextInt(100);
                                    s.aia1.set(0, p);
                                    sleep(2); // pad the writes a bit to make total write time longer
                                    s.aia1.set(1, p);
                                    sleep(2);
                                    s.aia1.set(2, p);
                                    sleep(2);
                                    s.aia1.set(3, p);
                                    sleep(2);
                                    s.aia1.set(4, p);
                                    sleep(2);
                                    s.aia1.set(5, p);
                               }
                               catch(InterruptedException ie){}
                               writes++;
                          }
                     }
                }
           }
      }
      ---------- END SOURCE ----------
      (Incident Review ID: 275471)
      ======================================================================

            martin Martin Buchholz
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: