• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.util.Collections is a huge load of ****?  RSS feed

 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

A subtle, yet very important observation imo.
 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want a piece of me?

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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hah if only everyone coded like that, we'd be 20 years ahead in our software
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?)
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."


sev
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!