I am trying to use Collections.getSynchronizedCollection() in order to make my collection synchronized, because I get the following error:
Here's the code where I get it:
And here's the only thing that could possibly be causing this:
So, I used a whole bunched of synchornized statemetns, 2 synchronized colelctions, probably trashing the performance of my program, YET I still get that stupid error. Anybody knows what could be the problem?
Ernest Friedman-Hill
author and iconoclast
Marshal
There are two synchronized blocks, but they're each synchronized on a different object, so they don't "see" each other, and can still both run at once. If all we need to worry about are these two chunks of code, then I'd dispense with the synchronizedCollection(), and synchronize both blocks on the fileMap instance itself (or any other handy object that is accessible to both blocks of code.)
And are will still going on about ****? Our previous conversation wasn't about censorship. It wasn't about bleeping out curse words. It was about being nice. If JavaRanch were a movie, it would be "A Fistful of Dollars," not "Unforgiven" with the curse words bleeped out. [ June 22, 2004: Message edited by: Ernest Friedman-Hill ]
From the JavaDoc for Collections.synchronizedCollection():
In order to guarantee serial access, it is critical that all access to the backing collection is accomplished through the returned collection. (Emphasis theirs)
It looks like the second code snippet you posted is circumventing the synchronised wrapper.
--Tim
Ivan Jouikov
Ranch Hand
Joined: Jul 22, 2003
Posts: 269
posted
0
OMG I AM SO HAPPY! I FIXED THAT SUCKER!
#1 Problem was that I said "And here's the only thing that could possibly be causing this:", which, of course, it wasn't
The problem was that inside THAT SAME synchronized{} block I deleted an element from the fileMap, which violated the structural integrity of my HashMap (or whatever it is), and that's why it was throwing those exceptions...
About ****, I think you could give me some credit for not snapping and using **** . OK OK, if you're that picky I'll be as nice as if you were my gramma.
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
You might also consider changing your attitude for reasons that are beneficial only to yourself.
Consider the amount of wasted time you spent blaming the Collections API for your screw up. The Collections API was doing as it was told - you weren't - it didn't call you a **** - it just kept on working. Continually blaming "something else" is a very subtle trap that a lot of newcomers experience.
I see what you're saying, it's just that I spent 2 hours trying to figure out what was wrong, and it really pissess me off. It's the simplest mistakes that are the most annoying.
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
The second sentence in the javadoc for ConcurrentModificationException - surely not an unreasonable place to look - reads For example, it is not generally permssible for one thread to modify a Collection while another thread is iterating over it. Seems like exemplary error handling and documentation.
Having said that, we all know the sinking feeling of realizing that we missed something really obvious that was there all the time with a neon flashing sign for us to see
- Peter [ June 23, 2004: Message edited by: Peter den Haan ]
Chong-Leng Goh
Greenhorn
Joined: Apr 20, 2004
Posts: 7
posted
0
Coding is an enjoyment and I have to admit the saisfaction when you managed to resolve a problem that has been haunting you for hours/days/even months is incredibly pleasure. But if you are stuck with some portion of your program during coding for too long, it is always fruitful to take some time out to relax.
Sometime after looking at a problem for too long, it pays to talk to someone and try to explain to them what you are trying to do. More often than not, you will immediately see the things you may have missed out.
Having say that, I have to admit it is sometime difficult to relax when you are all 'high' over your codes.
I know what you mean. Some time tension is so high and you feel like u need just 1 more minute and you'll fix that problem, and u end up spending hours stressing out and end up not fixing anything.
But when you DO fix it, you're like all YES!!! And throw your hand up, and go grab a BB gun and shoot some birds (that's what I do at least ))
Tim West
Ranch Hand
Joined: Mar 15, 2004
Posts: 539
posted
0
LOL. Is anyone else reminded of the quote: "Always code as if the guy who'll be maintaining your code is a deranged psychopath who knows where you live"...or something like that?
Ivan Jouikov
Ranch Hand
Joined: Jul 22, 2003
Posts: 269
posted
0
Hah if only everyone coded like that, we'd be 20 years ahead in our software
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
Always code as if the guy who'll be maintaining your code is a deranged psychopath who knows where you live
Why do you say "as if"? Is this not the case where you work? (Are they hiring?)
"I'm not back." - Bill Harding, Twister
Tim West
Ranch Hand
Joined: Mar 15, 2004
Posts: 539
posted
0
Nah it isn't like that here...I'm maintaining the code and I don't know where the original developers live. Dammit!
~Tim
Jeroen Wenting
Ranch Hand
Joined: Oct 12, 2000
Posts: 5093
posted
0
Originally posted by Tim West: LOL. Is anyone else reminded of the quote: "Always code as if the guy who'll be maintaining your code is a deranged psychopath who knows where you live"...or something like that?
I maintain my own code, does that make me a psychopath? I do know where I live so I'd better be careful
42
sever oon
Ranch Hand
Joined: Feb 08, 2004
Posts: 268
posted
0
Actually, I was reminded of a different quote: "Give a man a program and you'll frustrate him for a day. Teach a man to program, though, and you'll frustrate him for a lifetime."