Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Synchronize an iterator in java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 18-02-2010
Member
 
Join Date: Dec 2009
Posts: 192
Synchronize an iterator in java

Hello,
I have some code which used hashtable and the enumeration. I converted all the hashtable in treemap and enumeration iterator to return, however, at a specific location.
Here is the code for it
Code:
Iterator = it son.keySet().iterator();
            
            while (it.hasNext()) {
               String cleTemp = (String)it.next();
                Fil fl = ((Thread)son.get(cleTemp));
                if (fl.estRelieA(codeElem))    supprimerFil(cleTemp);
            }
I think the iterator is not synchronizing and I am getting an error. If you know the solution for this then please do post back.
Reply With Quote
  #2  
Old 18-02-2010
Member
 
Join Date: Dec 2009
Posts: 192
Re: Synchronize an iterator in java

Hello,
I am new to this , but I have a code with me, you can try it
See below for the code
Code:
Set s = Collections.synchronizedSet(son.keySet()); 
         synchronized(s) {
        Iterator it = s.iterator();
            
            while (it.hasNext()) {
               String str = (String)it.next();
                Fil fl = ((Thread)son.get(str));
                if (fl.est(codeElem))    supprimerFil(str);
            }
         }
Reply With Quote
  #3  
Old 18-02-2010
Member
 
Join Date: Nov 2009
Posts: 520
Re: Synchronize an iterator in java

Hello,
Alternatively you can try the following
Code:
private void supprimerFil(String cdfl)  {
        Fil fl = ((Thread)sn.get(cdfl));
        fl.deconnecterFil(elements, sn);
        Intl srt = fl.getSortie();
        sn.remove(cdfl);
        
        / / We delete isolated nodes
       elmn();
        
        updateGrilleTraces();
Just have a try at it and see if it works , and if not then post back with the error you are getting and we will try to solve it.
Reply With Quote
  #4  
Old 18-02-2010
Member
 
Join Date: Nov 2009
Posts: 335
Re: Synchronize an iterator in java

Hello,
What I mean Zekey is that during the course of a collection with an iterator, it is forbidden to modify the collection. This is not a timing issue but many multithreaded algorithm.
Code:
I am using a fl.remove (key) in the method supfl
Precisely the error comes from there, You should see it
Code:
clefsFils.remove();
Reply With Quote
  #5  
Old 18-02-2010
Member
 
Join Date: Dec 2009
Posts: 192
Re: Synchronize an iterator in java

Hello,
Thanks for the help, I have tried a code please check it and see if it is correct and if you find any problem in the code then guide me.
here is the code:
Code:
private void supfl(cdfl String, Iterator clfl)  {
        Fil flsup = ((Thread)son.get(cdfl));
        flsup.deconnecterFil(elements, son);
        Intl srt = flsup.getSortie();
        
        clfl.remove();
        
        / / We delete isolated nodes
        eliminerNoeudsIsoles();
        
        updateGrilleTraces();
    }
Reply With Quote
  #6  
Old 18-02-2010
Member
 
Join Date: Dec 2009
Posts: 204
Re: Synchronize an iterator in java

Hello,
I am new to this and even I am trying some programs on this, I have a small part of the code , which I have displayed here
Code:
synchronized(ls) {
Iterator i = ls.iterator(); // synchronized block
while (i.hasNext())
foo(i.next());
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Synchronize an iterator in java"
Thread Thread Starter Forum Replies Last Post
How to check if the iterator points on an object in a list? MAGALY Software Development 5 02-03-2010 07:59 PM
Designing LIFO and Iterator Remedy Software Development 5 23-02-2010 05:57 AM
Iterator interface problem in an ArrayList Ash maker Software Development 5 09-02-2010 06:06 AM
What is the difference between Iterator and Enumeration in Java? Karsenman Software Development 4 28-01-2010 09:56 AM
How do you synchronize java? Luis-Fernando Software Development 5 28-11-2009 06:07 PM


All times are GMT +5.5. The time now is 03:40 AM.