Scorpius 144 Author 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.
GetBig 1 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
Scorpius 144 Author Posted January 11, 2018 not rlly, it didnt take any effort for me to type that comment just leaving this there for memories
Dinh 496 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.
Scorpius 144 Author 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
Dinh 496 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
Scorpius 144 Author 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!
dQw4w9WgXcQ 184 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)
Scorpius 144 Author 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.
Scorpius 144 Author 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
Recommended Posts
Archived
This topic is now archived and is closed to further replies.