Article View: borland.public.jbuilder.java.language
Article #7378Re: HashMap.keySet().iterator() and threads
From: Petar Banicevic
Date: Mon, 25 Jun 2001 12:44
Date: Mon, 25 Jun 2001 12:44
36 lines
1974 bytes
1974 bytes
Damn !! I haven't see that bug - thanks for that iterator and for exception.
I am developing this code last 15 days, and I can't test it because I need
to finish client side too, so I have to finish this first...
"Torsten Welches" <torsten.welches@who.cares.netcologne.de> wrote in message
news:3b370a9b$1_1@dnews...
>
> "Petar Banicevic" <petar_b@usa.net> wrote:
>
> >while (localStack.keySet().iterator().hasNext()) {
> > String fileName=(String)localStack.keySet().iterator().next();
> >}
> >
> >Now, what would happen if somebody else, out of this thread,
> >adds/removes a few lines in localStack while this loop is >running.
LocalStack is HashMap.
>
> a java.util.ConcurrentModificationException would be thrown.
>
> BTW - you code does probably not what you expect it to do.
> In your while body you get a *new* iterator each time and call
> next on that. You would end up getting the first item all the
> time.
>
> I guess that's what you want instead:
>
> Iterator it = localStack.keySet().iterator();
> while (it.hasNext()) {
> String fileName=(String)it.next();
> }
>
> Torsten
Technical Details
Message-ID:
<3b37321e_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnewsReferences:
<3b36ff40_1@dnews> <3b370a9b$1_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,2ab9a045bec56d80 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-06-25 05:59:51 PST From: "Petar Banicevic" <petar_b@usa.net> Newsgroups: borland.public.jbuilder.java.language References: <3b36ff40_1@dnews> <3b370a9b$1_1@dnews> Subject: Re: HashMap.keySet().iterator() and threads Date: Mon, 25 Jun 2001 14:44:05 +0200 Lines: 38 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 NNTP-Posting-Host: 12.98.142.222 Message-ID: <3b37321e_1@dnews> X-Trace: dnews 993473054 12.98.142.222 (25 Jun 2001 05:44:14 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews