Scorpius 144 Author Share Posted January 11, 2018 Why does it have to be on dreambot ??? Its a general java programming practice, just google concurrency in java lol What I mean is, the effort you spend on criticizing can be spent on something more productive. Link to comment Share on other sites More sharing options...
GetBig 1 Share Posted January 11, 2018 What I mean is, the effort you spend on criticizing can be spent on something more productive. not rlly, it didnt take any effort for me to type that comment Link to comment Share on other sites More sharing options...
Scorpius 144 Author Share Posted January 11, 2018 not rlly, it didnt take any effort for me to type that comment just leaving this there for memories Link to comment Share on other sites More sharing options...
Dinh 496 Share Posted January 14, 2018 Just use a fori loop, the CurrentModificationException is thrown because you are editing the List in a for-each loop which uses an iterator. The iterator checks if something changed on every iteration, meaning as soon as you remove or add something the next iteration will throw this error. Using a fori loop you evade this iterator being used resulting in no error being thrown. Link to comment Share on other sites More sharing options...
Scorpius 144 Author Share Posted January 14, 2018 Just use a fori loop, the CurrentModificationException is thrown because you are editing the List in a for-each loop which uses an iterator. The iterator checks if something changed on every iteration, meaning as soon as you remove or add something the next iteration will throw this error. Using a fori loop you evade this iterator being used resulting in no error being thrown. But removing from the list while the loop is running somewhere still throws an exception, I'd solve it the same way I am solving it now Link to comment Share on other sites More sharing options...
Dinh 496 Share Posted January 14, 2018 But removing from the list while the loop is running somewhere still throws an exception, I'd solve it the same way I am solving it now Stop using for-each loops and use fori instead If you are multi-threading and accessing this list from multiple Threads use synchronized blocks eventhough you should be fine only using fori Link to comment Share on other sites More sharing options...
Scorpius 144 Author Share Posted January 14, 2018 Stop using for-each loops and use fori instead If you are multi-threading and accessing this list from multiple Threads use synchronized blocks eventhough you should be fine only using fori I'll definitely be doing those very soon, thank you fella! Link to comment Share on other sites More sharing options...
dQw4w9WgXcQ 184 Share Posted January 14, 2018 just use List#addAll avoiding iterator alone isnt thread safe. you still should use a concurrent collection or synchronized block. a concurrent collection makes more sense in this case. CopyOnWriteArrayList (or ConcurrentLinkedQueue if u dont need indexed access) Link to comment Share on other sites More sharing options...
Scorpius 144 Author Share Posted January 14, 2018 just use List#addAll avoiding iterator alone isnt thread safe. you still should use a concurrent collection or synchronized block. a concurrent collection makes more sense in this case. CopyOnWriteArrayList (or ConcurrentLinkedQueue if u dont need indexed access) I'm adding/removing/reading at the same time on 3 different threads, still thinking about approaches tbh but every answer in here is a step forward. Link to comment Share on other sites More sharing options...
Scorpius 144 Author Share Posted January 26, 2018 I'll be writing a tutorial on concurrency/object locking/mutex in the general programming section this week, if you really intend to learn where's ur guide big mouth Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.