array -- synchronized? expandable?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: array -- synchronized? expandable?

  1. #1
    Join Date
    Jul 2004
    Posts
    54

    Question array -- synchronized? expandable?

    I have a routine I wrote a long time ago that use primitive arrays -- int a[][] , String sa[], double d[][]. The ones with [][] are really ararys of arrays not 2d arrays (x-y).

    My question is are these synchronized so different threads have to wait their turn.

    How might I extend the [][] array of arrays as well as the simpler a[] and sa[]?

    I've looked at Vectors (synchronized) and the others (non-sync'd). How might one handle the array of arrays with Vectors?

    I'm preparing a short sample to add to this and should have by tomorrow morning.

    Thanks,
    RON C

  2. #2
    Join Date
    Jul 2004
    Posts
    54
    Attached file is really a java file with txt as file type with the code I've described above.

    Is it thread-safe? How can I extend the length of the arrays?

    Thanks,

    RON C
    Attached Files Attached Files

  3. #3
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    Arrays of arrays belong in C, not Java. You should use one of the various collections, in your case one of the synchronized collections like Vector. If you put arrays into a Vector, viola! you've got an array of arrays only object oriented. (In general you're better off staying away from native types entirely except when they're required for low level interface stuff.)

    Oh man that's some ugly code in that attachment. It's basically just a C module.

  4. #4
    Join Date
    Jul 2004
    Posts
    54
    Thanks.

    The ugly code is very old c code.

    I'm just learning about collections etc. so I appreciate your help.

    RON C

  5. #5
    Join Date
    Jul 2004
    Posts
    54
    Ray,

    How would you change my ugly code to use Vector for a key/value setup? Especially where value is an array of int or other primitive types.

    I'm totally lost with collections etc. right now.

    Thanks,
    RON C

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    This is how I would replace that pile of .... code

    I have not implemented the whole set of setters and getters but the
    general layout should be like this.

    Code:
    import java.util.*;
    
    
    /**
     *
     * Exception used for get operations
     */
    class GPException extends Exception {
      public final static int UNDEFINED=0;
      public final static int DATA_NOT_FOUND=1;
      public final static int INVALID_DATA_TYPE=2;
    
      private int errorType=UNDEFINED;
      public GPException(int errorType, String message) {
        super(message);
        this.errorType=errorType;
      }
      public void showError() {
        String s="Error: ";
        switch (errorType) {
          case UNDEFINED:
            s+=" *undefined* ";
            break;
          case DATA_NOT_FOUND:
            s+=" Data not found ";
            break;
          case INVALID_DATA_TYPE:
            s+=" Invalid data type ";
            break;
        }
        s += getMessage();
      }
    }
    
    
    /**
      GlobalParms storage routine
      set a parameter --  GlobalParms.xxset_gp("name", value);
      get a parameter --  value = GlobalParms.xxget_gp("name");
    
      where -- xx before set and get is the data type:
        single value
          b = boolean, by = byte, c = char,
          i  = int, sh = short, lo = long,
          f = float, d = double, s = String
        array of values
          ba = boolean, bya = byte, ca = char,
          ia  = int, sha = short, loa = long,
          fa = float, da = double, sa = String
     */
    
    
    
    public class GlobalParameters
        extends Hashtable {
      /**
       * Uses default load factor (0.75)
       * @param maxEntries
       */
      public GlobalParameters (int maxEntries) {
        super(maxEntries);
      }
      /**
       *
       * @param name
       * @param value
       */
      public void setGP(String name, int value) {
        put(name,new Integer(value));
      }
    
      public int getGP(String name) throws GPException {
        Object ob=get(name);
        if (ob==null) {
          throw new GPException(GPException.DATA_NOT_FOUND, name);
        }
        try {
          return ( (Integer) ob).intValue();
        } catch (ClassCastException ex) {
          throw new GPException(GPException.INVALID_DATA_TYPE, name);
        }
      }
    
      public void aSetGP(String name, int[] value) {
        put(name,value);
      }
    
      public int[] aGetGP(String name) throws GPException {
        Object ob=get(name);
        if (ob==null) {
          throw new GPException(GPException.DATA_NOT_FOUND, name);
        }
        try {
          return (int[]) ob;
        } catch (ClassCastException ex) {
          throw new GPException(GPException.INVALID_DATA_TYPE, name);
        }
      }
    
    
      public boolean gpExists (String name) {
        return get(name) != null;
      }
    
      public int findGP(String name, String testName[]) {
        for (int i=0; i<testName.length; i++) {
          if (name.equals(testName[i])) {
            return i;
          }
        }
        return -1;
      }
    
       //***************************
       // main routine for stand alone test
        public static void main(String[] args) {
         try {
           int vout;
           GlobalParameters gp = new GlobalParameters(100);
           gp.setGP("test2", 25);
           vout = gp.getGP("test2");
           System.out.println(" here1 " + "test2" + " " + vout);
           gp.setGP("test2", 50);
           vout = gp.getGP("test2");
           System.out.println(" here2 " + "test2" + " " + vout);
           gp.setGP("test1", 75);
           //
           vout = gp.getGP("test2");
           System.out.println(" here3 " + "test2" + " " + vout);
           vout = gp.getGP("test1");
           System.out.println(" here4 " + "test1" + " " + vout);
           int arr[] = {
               0, 1, 2, 3};
           int vrr[];
           gp.aSetGP("testi", arr);
           vrr = gp.aGetGP("testi");
           System.out.println(" here5 " + "testi" + " " + arr[0] + " " + arr[1]
                              + " " + arr[2] + " " + arr[3] + " " + vrr.length +
                              " " +
                              vrr[0]
                              + " " + vrr[1] + " " + vrr[2] + " " + vrr[3]);
         } catch (GPException ge) {
           ge.showError();
         }
       }
    }
    Last edited by sjalle; 06-02-2005 at 08:57 AM.
    eschew obfuscation

  7. #7
    Join Date
    Jul 2004
    Posts
    54
    Ray,
    Thanks so much.

    I'm learning a great deal from what you supplied. Pretty simple once it was coded. I need to get a better text (Core Java2 version 1.5), study it and work on this more. My code was developed back on a PDP (model 8 or 11 - can't remember) and the c version wasn't very versatile.

    Is your code synchronized/thread-safe? Core Java says that only Vector is synchronized and the others can be by adding extra code in each routine.

    Where do you live in TX? I'm in Conroe.

    Really appreciate you help.

    RON C

  8. #8
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I'm not sure what you mean by thread safe, this is not a multi threaded application is it ?
    If multiple simultaneous processes were to access this, also for element deletiion, I
    would have wrapped the access in one synchronized method using an additional
    access type parameter.

    Texas, nope, I live in Norway.
    eschew obfuscation

  9. #9
    Join Date
    Jul 2004
    Posts
    54
    Thanks for the response. You've bailed me out before.

    I use the ugly C code in a number of algorithms and some are multi-threaded. That was one of the reasons I started this thread. Could you easily show me what is needed for the synchronized version?

    I have already insert the code for other data types and it seems to work well. I've even made an interface to link this to the old code replacing my ugly code.



    Thanks again.

    RON C

    The part about Texas was for Ray326 as I didn't pay attention to whom had sent the new code.

  10. #10
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    The sync will have to be for the methods that add and remove elements, so you could
    make one synchronized method called updateGP that took three parameters and
    handled both add & remove;
    Code:
    public synchronized void updateGP(int updateType, int name, Object value) throws GPException {
     .
     .
    }
    This method could be used for both add and remove operations, for the remove
    operation the last parameter would be null (and that would be the only one to
    throw a not-found exception)
    Last edited by sjalle; 06-02-2005 at 08:57 PM.
    eschew obfuscation

  11. #11
    Join Date
    Jul 2004
    Posts
    54
    Thanks.

    I think I understand what you're recommending. I'll have a go at it.

    RON C

  12. #12
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    Ray,
    Thanks so much.
    ...
    Where do you live in TX? I'm in Conroe.
    It's our friendly neighborhood moderator that deserves the thanks. I'm in Arlington, BTW.

  13. #13
    Join Date
    Jul 2004
    Posts
    54
    Ray,

    I did realize that you were not the one who had responded and apologied to SJalle.
    I also thanked him.

    You guys are real helpfu even thoigh my code is real ugly. That ugly code might be older than most on this site as I coded it sometime around 1970 - maybe earlier for PDP 8 or 11 and bastardized Fortran and C code. I can't remember which machine. I was a researcher who needed some table lookup code and we kludged tht together.

    RON C

  14. #14
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    LOL. Yeah, I've got code older than the kids I work with, too.

  15. #15
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Fortran huh ?

    How about 45000 lines of fortran code with lots of parameterless subroutines
    and the whole variables ballgame stuffed into one big fat common area, in
    order to calculate the mass balance on the Ekofisk oil field... ?

    Good old days ... ? NOT !
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center