Why am i getting ConcurrentModificationException on this unmodifiableSet?
I'm getting a java.util.ConcurrentModificationException on the line where the for-loop starts (see comment in code).
Why am i getting ConcurrentModificationException on this unmodifiableSet?
final Set<Port> portSet = Collections.unmodifiableSet(node.getOpenPorts());
if (!portSet.isEmpty())
StringBuilder tmpSb = new StringBuilder();
for (Port pp : portSet) // <------- exception happening here
tmpSb.append(pp.getNum()).append(" ");
I've never witnessed this, but I'm getting crash reports from Google.
java
add a comment |
I'm getting a java.util.ConcurrentModificationException on the line where the for-loop starts (see comment in code).
Why am i getting ConcurrentModificationException on this unmodifiableSet?
final Set<Port> portSet = Collections.unmodifiableSet(node.getOpenPorts());
if (!portSet.isEmpty())
StringBuilder tmpSb = new StringBuilder();
for (Port pp : portSet) // <------- exception happening here
tmpSb.append(pp.getNum()).append(" ");
I've never witnessed this, but I'm getting crash reports from Google.
java
1
Can theSetreturned bynode.getOpenPorts()be modified by other code (not necessarily your own code)?
– Slaw
Nov 11 '18 at 2:00
ok. i guess my assumption ofCollections.unmodifiableSetmaking a copy of the set is wrong. I guess it just wraps and prevents add/removes?
– Nick
Nov 11 '18 at 2:30
Yes, theCollections.unmodifiableXXXmethods all wrap the given collection. These wrappers delegate to the underlying collection for read operations, but throwUnsupportedOperationExceptionfor the write operations.
– Slaw
Nov 11 '18 at 2:53
1
The fine documentation.
– chrylis
Nov 11 '18 at 2:54
add a comment |
I'm getting a java.util.ConcurrentModificationException on the line where the for-loop starts (see comment in code).
Why am i getting ConcurrentModificationException on this unmodifiableSet?
final Set<Port> portSet = Collections.unmodifiableSet(node.getOpenPorts());
if (!portSet.isEmpty())
StringBuilder tmpSb = new StringBuilder();
for (Port pp : portSet) // <------- exception happening here
tmpSb.append(pp.getNum()).append(" ");
I've never witnessed this, but I'm getting crash reports from Google.
java
I'm getting a java.util.ConcurrentModificationException on the line where the for-loop starts (see comment in code).
Why am i getting ConcurrentModificationException on this unmodifiableSet?
final Set<Port> portSet = Collections.unmodifiableSet(node.getOpenPorts());
if (!portSet.isEmpty())
StringBuilder tmpSb = new StringBuilder();
for (Port pp : portSet) // <------- exception happening here
tmpSb.append(pp.getNum()).append(" ");
I've never witnessed this, but I'm getting crash reports from Google.
java
java
asked Nov 11 '18 at 1:54
NickNick
1,08211321
1,08211321
1
Can theSetreturned bynode.getOpenPorts()be modified by other code (not necessarily your own code)?
– Slaw
Nov 11 '18 at 2:00
ok. i guess my assumption ofCollections.unmodifiableSetmaking a copy of the set is wrong. I guess it just wraps and prevents add/removes?
– Nick
Nov 11 '18 at 2:30
Yes, theCollections.unmodifiableXXXmethods all wrap the given collection. These wrappers delegate to the underlying collection for read operations, but throwUnsupportedOperationExceptionfor the write operations.
– Slaw
Nov 11 '18 at 2:53
1
The fine documentation.
– chrylis
Nov 11 '18 at 2:54
add a comment |
1
Can theSetreturned bynode.getOpenPorts()be modified by other code (not necessarily your own code)?
– Slaw
Nov 11 '18 at 2:00
ok. i guess my assumption ofCollections.unmodifiableSetmaking a copy of the set is wrong. I guess it just wraps and prevents add/removes?
– Nick
Nov 11 '18 at 2:30
Yes, theCollections.unmodifiableXXXmethods all wrap the given collection. These wrappers delegate to the underlying collection for read operations, but throwUnsupportedOperationExceptionfor the write operations.
– Slaw
Nov 11 '18 at 2:53
1
The fine documentation.
– chrylis
Nov 11 '18 at 2:54
1
1
Can the
Set returned by node.getOpenPorts() be modified by other code (not necessarily your own code)?– Slaw
Nov 11 '18 at 2:00
Can the
Set returned by node.getOpenPorts() be modified by other code (not necessarily your own code)?– Slaw
Nov 11 '18 at 2:00
ok. i guess my assumption of
Collections.unmodifiableSet making a copy of the set is wrong. I guess it just wraps and prevents add/removes?– Nick
Nov 11 '18 at 2:30
ok. i guess my assumption of
Collections.unmodifiableSet making a copy of the set is wrong. I guess it just wraps and prevents add/removes?– Nick
Nov 11 '18 at 2:30
Yes, the
Collections.unmodifiableXXX methods all wrap the given collection. These wrappers delegate to the underlying collection for read operations, but throw UnsupportedOperationException for the write operations.– Slaw
Nov 11 '18 at 2:53
Yes, the
Collections.unmodifiableXXX methods all wrap the given collection. These wrappers delegate to the underlying collection for read operations, but throw UnsupportedOperationException for the write operations.– Slaw
Nov 11 '18 at 2:53
1
1
The fine documentation.
– chrylis
Nov 11 '18 at 2:54
The fine documentation.
– chrylis
Nov 11 '18 at 2:54
add a comment |
1 Answer
1
active
oldest
votes
Something must be modifying the underlying set; i.e. the set returned by node.getOpenPorts().
Instead of wrapping the set with an "unmodifiable" wrapper, you could copy it.
final Set<Port> portSet = new HashSet<>(node.getOpenPorts());
But as a commenter (@Slaw) pointed out, that just moves the iteration inside the constructor and you would still get CCMEs.
The only real solutions are:
Change the implementation of the node class to use a concurrent set class for the port list that won't throw CCMEs if the collection is mutated while you are iterating it.
Change the implementation of the node class to return a copy of the port list. Deal with the updates-while-copying race condition with some internal locking.
Put a try / catch around the code and repeat the operation if you get a CCME
I've never witnessed this, but I'm getting crash reports from Google.
Yes. The problem only occurs if this code is executed while the open port list is changing.
Doesn't this just move the iteration to insideHashSet? Based on OP's code, the act of copying seems just as likely to throw aConcurrentModificationException.
– Slaw
Nov 11 '18 at 2:08
I'm marking this as the answer, because what I need to do is replace my TreeSet with a ConcurrentSkipListSet. TheCollections.unmodifiableSetis not needed. Thanks.
– Nick
Nov 11 '18 at 17:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245185%2fwhy-am-i-getting-concurrentmodificationexception-on-this-unmodifiableset%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Something must be modifying the underlying set; i.e. the set returned by node.getOpenPorts().
Instead of wrapping the set with an "unmodifiable" wrapper, you could copy it.
final Set<Port> portSet = new HashSet<>(node.getOpenPorts());
But as a commenter (@Slaw) pointed out, that just moves the iteration inside the constructor and you would still get CCMEs.
The only real solutions are:
Change the implementation of the node class to use a concurrent set class for the port list that won't throw CCMEs if the collection is mutated while you are iterating it.
Change the implementation of the node class to return a copy of the port list. Deal with the updates-while-copying race condition with some internal locking.
Put a try / catch around the code and repeat the operation if you get a CCME
I've never witnessed this, but I'm getting crash reports from Google.
Yes. The problem only occurs if this code is executed while the open port list is changing.
Doesn't this just move the iteration to insideHashSet? Based on OP's code, the act of copying seems just as likely to throw aConcurrentModificationException.
– Slaw
Nov 11 '18 at 2:08
I'm marking this as the answer, because what I need to do is replace my TreeSet with a ConcurrentSkipListSet. TheCollections.unmodifiableSetis not needed. Thanks.
– Nick
Nov 11 '18 at 17:53
add a comment |
Something must be modifying the underlying set; i.e. the set returned by node.getOpenPorts().
Instead of wrapping the set with an "unmodifiable" wrapper, you could copy it.
final Set<Port> portSet = new HashSet<>(node.getOpenPorts());
But as a commenter (@Slaw) pointed out, that just moves the iteration inside the constructor and you would still get CCMEs.
The only real solutions are:
Change the implementation of the node class to use a concurrent set class for the port list that won't throw CCMEs if the collection is mutated while you are iterating it.
Change the implementation of the node class to return a copy of the port list. Deal with the updates-while-copying race condition with some internal locking.
Put a try / catch around the code and repeat the operation if you get a CCME
I've never witnessed this, but I'm getting crash reports from Google.
Yes. The problem only occurs if this code is executed while the open port list is changing.
Doesn't this just move the iteration to insideHashSet? Based on OP's code, the act of copying seems just as likely to throw aConcurrentModificationException.
– Slaw
Nov 11 '18 at 2:08
I'm marking this as the answer, because what I need to do is replace my TreeSet with a ConcurrentSkipListSet. TheCollections.unmodifiableSetis not needed. Thanks.
– Nick
Nov 11 '18 at 17:53
add a comment |
Something must be modifying the underlying set; i.e. the set returned by node.getOpenPorts().
Instead of wrapping the set with an "unmodifiable" wrapper, you could copy it.
final Set<Port> portSet = new HashSet<>(node.getOpenPorts());
But as a commenter (@Slaw) pointed out, that just moves the iteration inside the constructor and you would still get CCMEs.
The only real solutions are:
Change the implementation of the node class to use a concurrent set class for the port list that won't throw CCMEs if the collection is mutated while you are iterating it.
Change the implementation of the node class to return a copy of the port list. Deal with the updates-while-copying race condition with some internal locking.
Put a try / catch around the code and repeat the operation if you get a CCME
I've never witnessed this, but I'm getting crash reports from Google.
Yes. The problem only occurs if this code is executed while the open port list is changing.
Something must be modifying the underlying set; i.e. the set returned by node.getOpenPorts().
Instead of wrapping the set with an "unmodifiable" wrapper, you could copy it.
final Set<Port> portSet = new HashSet<>(node.getOpenPorts());
But as a commenter (@Slaw) pointed out, that just moves the iteration inside the constructor and you would still get CCMEs.
The only real solutions are:
Change the implementation of the node class to use a concurrent set class for the port list that won't throw CCMEs if the collection is mutated while you are iterating it.
Change the implementation of the node class to return a copy of the port list. Deal with the updates-while-copying race condition with some internal locking.
Put a try / catch around the code and repeat the operation if you get a CCME
I've never witnessed this, but I'm getting crash reports from Google.
Yes. The problem only occurs if this code is executed while the open port list is changing.
edited Nov 11 '18 at 2:24
answered Nov 11 '18 at 2:01
Stephen CStephen C
516k69564921
516k69564921
Doesn't this just move the iteration to insideHashSet? Based on OP's code, the act of copying seems just as likely to throw aConcurrentModificationException.
– Slaw
Nov 11 '18 at 2:08
I'm marking this as the answer, because what I need to do is replace my TreeSet with a ConcurrentSkipListSet. TheCollections.unmodifiableSetis not needed. Thanks.
– Nick
Nov 11 '18 at 17:53
add a comment |
Doesn't this just move the iteration to insideHashSet? Based on OP's code, the act of copying seems just as likely to throw aConcurrentModificationException.
– Slaw
Nov 11 '18 at 2:08
I'm marking this as the answer, because what I need to do is replace my TreeSet with a ConcurrentSkipListSet. TheCollections.unmodifiableSetis not needed. Thanks.
– Nick
Nov 11 '18 at 17:53
Doesn't this just move the iteration to inside
HashSet? Based on OP's code, the act of copying seems just as likely to throw a ConcurrentModificationException.– Slaw
Nov 11 '18 at 2:08
Doesn't this just move the iteration to inside
HashSet? Based on OP's code, the act of copying seems just as likely to throw a ConcurrentModificationException.– Slaw
Nov 11 '18 at 2:08
I'm marking this as the answer, because what I need to do is replace my TreeSet with a ConcurrentSkipListSet. The
Collections.unmodifiableSet is not needed. Thanks.– Nick
Nov 11 '18 at 17:53
I'm marking this as the answer, because what I need to do is replace my TreeSet with a ConcurrentSkipListSet. The
Collections.unmodifiableSet is not needed. Thanks.– Nick
Nov 11 '18 at 17:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245185%2fwhy-am-i-getting-concurrentmodificationexception-on-this-unmodifiableset%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can the
Setreturned bynode.getOpenPorts()be modified by other code (not necessarily your own code)?– Slaw
Nov 11 '18 at 2:00
ok. i guess my assumption of
Collections.unmodifiableSetmaking a copy of the set is wrong. I guess it just wraps and prevents add/removes?– Nick
Nov 11 '18 at 2:30
Yes, the
Collections.unmodifiableXXXmethods all wrap the given collection. These wrappers delegate to the underlying collection for read operations, but throwUnsupportedOperationExceptionfor the write operations.– Slaw
Nov 11 '18 at 2:53
1
The fine documentation.
– chrylis
Nov 11 '18 at 2:54