Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

NX: URLyBird 1.1.3

 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Vlad Rabkin:
Hi,
I've decided to dig into nio, but I synchronize read and write explicitly. I hope not to be penaltied for it.
Could anyone comment my code: It is my first attempt to do it- It works, but I am not sure the
the code is good (e.g. I don't if I should use force() method):



Regards,
Vlad


Hi Vlad,
It looks good, but I'd like to make a few suggestions/observation.
1. synchronize your write method. It's only the reads that are ok to leave unsynchronized.
2. close yout FileChannel when you're done with it, and null it out.
3. Nice, liberal use of comments. I like that
All best,
M
 
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Max,
First many thanx for your comments!
[Max]It looks good, but I'd like to make a few suggestions/observation.
1. synchronize your write method. It's only the reads that are ok to leave unsynchronized.
2. close yout FileChannel when you're done with it, and null it out.
3. Nice, liberal use of comments. I like that
1. It is private write method. It is used by main public method, which is synchronized on the map.
2. Max, as told: I will follow your idea to use FileChannel, but I will synchronize read explicitely and I will use shared FileChannel object, which I will not open and close every time. That means I guess I should not close it here. Considering what I have told here, should call force() method?
3) Thanx for the compliment!
Sorry for stupid questions, I am just trying to drive the car after reading instructions, but having no practise with the goal to get a driver license
Vlad
 
Max Habibi
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't heard you ask a stupid question, or make a stupid comment, yet Vlad. As to your question.


2. Max, as told: I will follow your idea to use FileChannel, but I will synchronize read explicitely and I will use shared FileChannel object, which I will not open and close every time. That means I guess I should not close it here. Considering what I have told here, should call force() method?


Yes, do use force, just for good measure. You shouldn't have to, but it doesn't hurt.
M
 
Vlad Rabkin
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Max,
Just to make sure I understand what I mean in �1:

Regards,
Vlad
 
Max Habibi
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks fine. I'd probably do a notifyAll on cashedDB_ before exiting the synch block, but that's all.
Nice!
M
 
Vlad Rabkin
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Max,

Many Thanx for your help!
Have a nice weekend.
Vlad
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've extracted a bunch of the posts that were specifically about FileChannel's "thread safety" and put them here. Please follow up that particular topic in that thread; other topics here. at the moment, all the original posts are still in this thread as well as having duplicates in the new thread. I'm inclinded to delete the duplicates from this thread, to clean things up, but others may feel their posts should be left here. So for the moment, I'm leaving this thread alone - but if no one objects I may delete the originals here for any posts that were copied to the other thread. Let me know if that seems undesireable...
    Bookmark Topic Watch Topic
  • New Topic