Under what circumstances would an unsynchronized collection, say an ArrayList, cause a problem? I can't think of any, can someone please give me an example where an ArrayList causes a ... |
I was reading about CopyOnWriteArrayList and was wondering how can I demonstrate data race in ArrayList class. Basically I'm trying to simulate a situation where ArrayList fails so that it becomes ... |
I have a Server class and a Timer inside it which is supposed to clear dead clients (clients who crashed). I followed the example below by locking the collection when the ... |
I have ArrayLists that store many objects, and objects are frequently added and removed from the ArrayLists. One thread operates on the data structures and updates the ArrayList's objects every 20ms ... |
JAVA
I have an ArrayList in my game to store all the particles ingame. I have an update method which accesses the ArrayList to update the physics, I have a render method ... |
Ive got one question. What happens when I try to add the "same" object twice to an ArrayList. With "the same" I mean an object of an individual class, which is ... |
Sup guys.
I'm writing a simple zombie game for my Java classes and I'm struck with a little problem: the Zombies are a class I made and I'm treating them on an ... |
|
I have a driver class called Advantech which is stored inside an ArrayList. I instantiate and initialize my class. The Advantech class extends Thread and the Advantech class has a run() ... |
EDIT: (QUESTION ANSWERED)
So I have a Thread controlling x and y and making it change. My problem is that when the snakes body gets put up in the GUI it puts ... |
i am trying to make a java application with GUI.
i am writing a code that i want to let the User change some data and save these changes on a ... |
I have an ArrayList that I want to iterate over it. While iterating over it I have to remove elements at the same time. Obviously this trhows an java.util.ConcurrentModificationException.
What is ... |
|
Well, if you have multiple threads(i mean, not taking the same runnable parameter), then, you can synchronize the getter and setter methods at the class level. synchronized(Myclass.class) {// code goes here } If all the thread objects access this class where the ArrayList object is there, then, you can synchronize the getter and setter methods at the object level. synchronized(this) {// ... |
[Nitesh]: BTW, you do not need to do anything on your own. Collections.synchronizedList(java.util.List) will do it for you. Hm, I doubt it. Usually you need to add explicit, external synchronization somewhere, in order to get useful, reliable results. Well, maybe if you put in some other restrictions, things "no more than one thread may ever remove elements from the list", or ... |
import java.util.*; public class TheClass implements Runnable { List arrList = new ArrayList(); int total; public void run() { for(int i : this.arrList) { this.total += i; } System.out.println(this.total + ", " + Thread.currentThread().getName()); } public static void main(String[] args) { TheClass obj = new TheClass(); obj.arrList.add(3); obj.arrList.add(5); obj.arrList.add(6); Thread t1 = new Thread(obj, "OurThread"); t1.start(); // You can do rest ... |
Hello, I'm newbie. So, excuse me if my question is stupid. Sorry for my english, too. I'm trying to build this functionality: Let's say that we have two ArrayLists: - keywordsToProcess - currentKeywords The idea is that I want to process keywordsToProcess with countless threads. As keywordsToProcess processing is limited to taking the first element (index 0), then I do things ... |
Hi All, I have code that looks like this: List studentsList = getStudentsList(); List allStudents = new ArrayList(); if (studentsList!=null && studentsList.size()>0){ for(Object d: studentsList){ if(d instanceof Student){ ((Student)d).setType("Fresher"); ((Student)d).setDesc("This is a newly joined student"); allStudents.add(d); } } } Now, my question is: 1. I saw some intermittent errors which complained about java.util.ConcurrentModificationException. Is the above code not threadsafe? 2. Can ... |
|
|
It may be just me, but I'm having a tough time figuring out exactly what you are trying to do based on your explanation. Perhaps you need to write out a more complete paragraph that explains what you are trying (in non-programming terms) as if we were 12 year olds. That is unless a more intelligent soul can make sense of ... |
First of all, put your program code between {code} tags. Nobody's going to read unformatted code. Secondly, IOOBE is a very simple to understand exception. You're accessing a collection beyond its limits. Thirdly, the stacktrace will tell you where this happens. If you have multiple threads involved, you might need appropriate synchronization in there. |