Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
  1. #21
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    Norm,

    When you say "update data" by this I believe you mean when it updates data in the HashMap, correct? because I am trying to chunk the hashmap values into lets say 10 threads, and pass each chunk of values from the hashmap to each thread, so no threads will operate on the same element of the hashmap at the same time. Am I assuming correctly?

    Thanks,

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    By "update data" I mean to change the value of any variables, including the contents of the HashMap.
    pass each chunk of values from the hashmap to each thread,
    I'd think a getNextJob() method would be better to allow to spread the load our. If one thread got the longer running jobs, another thread getting the shorter jobs could be finished.
    This all supposes that you can really overlap jobs.

  3. #23
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by new_2_java View Post
    When you say "update data" by this I believe you mean when it updates data in the HashMap, correct? ,
    I'm not Norm, but "update a HashMap" means anything that changes the HashMap. It can be adding or removing an entry, or changing an object in the map.

    You have to be very careful when you design objects that are keys in a HashMap, they should be immutable. It is critical that the values of equals() and hashCode() stay the same -- if they change, you may put something in a HashMap, change some internal field/member value which changes hashCode() and you will never find it again.

  4. #24
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    Norm,

    I modified your sample code, and added my code arround it.

    I am testing it with two threads and two element in the HashMap. So, either 1 thread should process both elements or each thread should process each element. However, for some reason both threads process both elements. So, the outcome is 4 element, as apposed to 2.

    I am thinking maybe the way I am calling the "getNextJob()" method. This is how it's used.
    Java Code:
        public synchronized Integer getKeyIndex () {
            if (nextKey < keys.length) {
                return keys[nextKey++];
            } else {
                return null;
            }
        }
        
        public synchronized String getNextJob(Integer k){
            if (k == null) {
                return null;
            }
            int kVal = k.intValue();
            if(kVal < keys.length) {        
                if (!map.get(keys[kVal]).toString().equalsIgnoreCase("Processed")) { 
                    return (String)map.get(keys[kVal]);
                } 
            } 
            return null;
          }
    
        public synchronized void markMapElementAsProcessed (Integer indexKey) {
            map.put(indexKey, new String("Processed"));
        }
    I need to set the values in the map, so the next thread shouldn't access the same element twice. Here's what I think. I have tried it, but didn't work.

    I have included 2 more methods,
    - Integer getNextKey()
    - void markElementAsProcessed(Integer k)

    Will this work?

    Thanks,
    Last edited by new_2_java; 10-17-2008 at 08:21 PM.

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    Why do you pass an index to the getNextJob() method? That method should be the one to decide which is the next job.
    With your code, who deciding which is the next job?

    have you looked at my code? How does getNextJob decide which is the next job?

  6. #26
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    Norm,

    The way I am getting the next Job is still the same way. Let's consider this code snipt.
    Java Code:
    public synchronized String getNextJob(){
    	if(nextKey < keys.length) {
    		if (!map.get(keys[nextKey]).toString().equalsIgnoreCase("Processed")) {
    			String v = (String)map.get(keys[nextKey]);
    			map.put(keys[nextKey++], new String("Processed"));
    			return v;
    		} else {
    			return null;
    		}
    	} else { 
    		return null;
    	}
    }
    Basically, here what I wanted to do is, to mark each element of the HashMap as "Processed" so the next thread won't process it. But, it seens that even using this method, the items are processed twice (In this case which I have 2 threads).

    I think, when I pass a reference of the MigrationUtility Class to MigrationUtilityThread Class, then each instance of the thread will have its own reference of the HashMap, isn't it?

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    Try debugging your code by adding some println()s to it to show changes to variables etc.
    so the next thread won't process it.
    Who controls access to the elements in the HashMap?
    If only the getNextJob() method does, it should be easy for it NOT to give the same job out more than once.
    Marking those returned as processed is NOT necessary.
    I don't think my code did that.
    each instance of the thread will have its own reference of the HashMap
    Not if the HashMap is private.
    No thread should know about the HashMap. All they need to know is where to get the next job.

  8. #28
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    Norm,

    Your program is actually working just fine. It's when I add my own stuff for migrating contetn, then for some odd reason, it processes each item twice. I tried all kinds of SOPs in all the variables, but still can't see why its behaving like this.

    I will try some more debugging, and will post back if i find out anything.

    Thanks once again for your patience and participation.

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    my own stuff for migrating content
    How does that change the logic flow?

  10. #30
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    Norm,

    It didn't change the logic flow. The program works just fine. I was placing the loop in the wrong place. :-)

    So, here's the final version, which works like a charm. Thanks again Norm.

    I made alittle bit changes here and there. But the guts of the program logic is still the same. I used Stack as apposed to HashMap. And made getNextJob() method as static.

    Java Code:
        public static synchronized String getNextJob() {        
            return stack.pop().toString();
        }
    
        public void run() {        
            while (MigrationUtility.getStackSize() > 0) {
                String nextItem = MigrationUtility.getNextJob();
                this.processItem(nextItem, this.getName());  
            }
        }
    Thanks once again for your help, real appreciate it.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Hashmap to TXT and TXT to Hashmap
    By elvinny in forum Advanced Java
    Replies: 4
    Last Post: 02-16-2011, 11:12 PM
  2. hashmap
    By tOpach in forum New To Java
    Replies: 2
    Last Post: 09-24-2008, 12:55 PM
  3. what is hashmap
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-03-2007, 01:23 PM

Posting Permissions

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