• Post Reply Bookmark Topic Watch Topic
  • New Topic

Newbie that's lost in thread space  RSS feed

 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Ok, the story has changed as I've written this email. The store started with "I have a program that when it runs generates the exception java.util.ConcurrentModificationException".

So, I spent some time to try to write a example version of a program in order to illustrate the problem that occurs when it runs. Well, here is the example program, and by golly, the example program runs without error. So, now I've iterated back and forth between the example code and the actual program in order to either get the program to run sucessfully, or to get the example program to fail.

Below is the example code, and it currently runs without error.

The example code purpose is
Given: Temperature data measured every day in a number of cities, but some of the temperature data is faulty.
Given: Arbitrary sets of input and output cities.
Given: For the input cities, there is a small integer that represents the date range of interest.
Construct a two dimensional array of the temperature data for a set of input cities. The rows of the 2-d array correspond to dates, but only dates with valid input and output
temperatures. The columns of the 2-d array correspond to the input cities. For one input city, there will be a number of columns, corresponding to the small integer that represents the date range of interest. Each of the columns for one input city should be offset by one valid date.

I wanted to indicate the corresponding location of the error, but in the program there was an additional section on the application of the intersection dates. So, the program was modified to be more similar to the example, and now the program runs without error.

So, below is an example of a program running multiple treads using ExecutorService.



 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to indicate the corresponding location of the error, but in the program there was an additional section on the application of the intersection dates. So, the program was modified to be more similar to the example, and now the program runs without error.


So I am clear - that means you fixed the problem right? Good Job! That is one of the best parts of making a good forum post with an example - sometimes you fix the problem while trying to explain it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7743
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, he's trying to make the example code throw a ConcurrentModificationException.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case - the most common cause of ConcurrentModificationExceptions is that a collection is being modified while it is being iterated. Examples:
- While iterating over a collection in one thread, another thread inserts into the collection
- While iterating over a collection in one thread, another thread deletes from the collection
- While iterating over a collection in one thread, another thread re-arranges the collection
- While iterating over a collection, the same loop that iterates tries to do one of the above actions.

I notice the use of Maps, and Sets. The keySet() and valueSet() Sets that come from a Map are actually just views of the Map. So if you iterate over the keySet, and modify the Map, you may still get the ConcurrentModificationException.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve,

The program did have a ConcurrentModificationException and while trying to duplicate it in an example program, I removed unnecessary loops that modified the map by removing the intersection elements. The example program worked without those loops, so I went back to the actual program and removed the loops and they both run now.

So, you were correct to say
that means you fixed the problem right?


Stephan,
Thank you for taking it further one step further, so Steve replied with a list of the probable causes of a ConcurrentModificationException. That helps me understand what's going on.

I guess it was a little mis leading to state that the purpose of the example program was to demonstrate a ConcurrentModificationException. It would have been more correct to say that the purpose of the example was to simulate a larger program that has a ConcurrentModificationException.

Thanks guys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!