Article View: borland.public.jbuilder.java.language
Article #7373Re: HashMap.keySet().iterator() and threads
From: Torsten Welches
Date: Mon, 25 Jun 2001 09:55
Date: Mon, 25 Jun 2001 09:55
25 lines
1702 bytes
1702 bytes
"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:
<3b370a9b$1_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news.maxwell.syr.edu!news.mel.connect.com.au!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!dnewsReferences:
<3b36ff40_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 02:59:50 PST From: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Sender: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Reply-To: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Subject: Re: HashMap.keySet().iterator() and threads Newsgroups: borland.public.jbuilder.java.language X-User-Info: 193.158.55.74 193.158.55.74 References: <3b36ff40_1@dnews> NNTP-Posting-Host: 207.105.83.65 Message-ID: <3b370a9b$1_1@dnews> Date: 25 Jun 2001 02:55:39 -0700 X-Trace: dnews 993462939 207.105.83.65 (25 Jun 2001 02:55:39 -0700) Lines: 25 Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news.maxwell.syr.edu!news.mel.connect.com.au!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!dnews