Results 1 to 7 of 7
  1. #1
    Dan0100 is offline Member
    Join Date
    Aug 2010
    Posts
    18
    Rep Power
    0

    Default Reuse a list of Integers

    Hello,
    I have a (potentially) infinite list of Integers, one for each person current connected to my socket server. These numbers a short, starting at 0,1,2...etc

    The idea are these a temporary unique IDs that stay with a user for their duration on my server:
    When a user joins they are assigned the first available number.
    When a user leaves, their number is recycled and waits for assignment to someone else.

    My code allows for these numbers to be as long as I like but I want to keep them short to conserve the number of bytes sent to each client from my server.

    I need to reuse these numbers somehow. I have a list of clients in each room but no centralised list, even if I did I would need to run through every client that was on the server (could be many!) so I need a better way of finding the first available number.

    I had an idea of using something like:
    Java Code:
    boolean ids[]= = new boolean[100000];
    But it has problem in that it limits me to 100000 clients at any one time! I may have more but I certainly will have fewer in this case the array wastes memory.

    I'm just posting here because I'm sure this has already been solved but I had trouble finding any answers with google. Thanks for reading,
    Dan

  2. #2
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    How about using an ArrayList<Integer>?

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    well if they're just IDs, I would just create a random number. If it clahes with anything, make another random number. Or if it's possible, get the first 6 digits or something of the current tick and use that as an ID.

    EDIT: Well I don't know if ticks is supported or not so I would just use get System.nanoTime() or System.currentTimeMillies().
    Last edited by Lil_Aziz1; 08-19-2010 at 06:55 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    Dan0100 is offline Member
    Join Date
    Aug 2010
    Posts
    18
    Rep Power
    0

    Default

    Thanks imorio, Ok I did a quick read up:
    - At the moment I am using LinkedLists for my clients in rooms, this is good as I do a lot of iteration and removing from anywhere in the list (when a client leaves)
    - ArrayLists are faster in getting the data, but slower in removing elements from anywhere in the list.

    I will use an ArrayList as I will only be removing the last element(s) at any one time. Ill get back to you with the results (hopefully!).
    Thanks :)

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Dan0100 View Post
    Thanks imorio, Ok I did a quick read up:
    - At the moment I am using LinkedLists for my clients in rooms, this is good as I do a lot of iteration and removing from anywhere in the list (when a client leaves)
    - ArrayLists are faster in getting the data, but slower in removing elements from anywhere in the list.

    I will use an ArrayList as I will only be removing the last element(s) at any one time. Ill get back to you with the results (hopefully!).
    Thanks :)
    BitSets are cheaper than that (one bit per number).

    kind regards,

    Jos

  6. #6
    Dan0100 is offline Member
    Join Date
    Aug 2010
    Posts
    18
    Rep Power
    0

    Default

    JosAH - BitSets.

    My code if you are interested:
    Java Code:
    private BitSet uuid = new BitSet(10000);
    //
    //client join method:
    int i = uuid.nextClearBit(0);
    uuid.set(i, true);
    //i is now used as client's ID
    //
    //client leave method:
    uuid.set(client.getID(), false);
    Simple, perfect. Thank you JosAH.

    Also yes limited to 10000, but thats 10000 bits now objects floating around ;) I can stretch to 1million hehe.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Dan0100 View Post
    JosAH - BitSets.

    My code if you are interested:
    Java Code:
    private BitSet uuid = new BitSet(10000);
    //
    //client join method:
    int i = uuid.nextClearBit(0);
    uuid.set(i, true);
    //i is now used as client's ID
    //
    //client leave method:
    uuid.set(client.getID(), false);
    Simple, perfect. Thank you JosAH.

    Also yes limited to 10000, but thats 10000 bits now objects floating around ;) I can stretch to 1million hehe.
    Yup, that's the way to do it; 1 million users is just 256KB of storage (plus a few bytes for the BitSet object itself).

    kind regards,

    Jos

Similar Threads

  1. Struts: Reuse data across multiple ActionForms problem
    By Tequila_Burp in forum Web Frameworks
    Replies: 3
    Last Post: 12-12-2012, 11:04 AM
  2. How does ExecutorService + newFixedThreadPool "reuse" threads?
    By tropico in forum Threads and Synchronization
    Replies: 5
    Last Post: 06-06-2011, 11:05 AM
  3. read integers from txt
    By genji in forum New To Java
    Replies: 12
    Last Post: 05-27-2010, 10:33 PM
  4. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 11:29 AM
  5. Object Reuse
    By sankarguru in forum Advanced Java
    Replies: 4
    Last Post: 03-25-2009, 06:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •