• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RAF or FileChannel

 
Arun Kumar
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using NIO for reads and writes in my Data class. For every read and write i synchronize on a static RAF instance.
The question is should i synchronize on RAF (as i am doing now) or the unique FileChannel it has? Currently in all my methods i use RAF.getChannel() to get the filechannel associated with the RAF. At the end of the method i am NOT closing the filechannel. If i close the filechannel at the end of the method, the next person who wants the filechannel for someother operation throws IOException.
Is there any advantage or disadvantage or any performance issues the way i am doing? Will there be any gains if i synchronize on FileChannel instead of RAF?
Arun
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These two techniques are almost exactly equivalent in effect, as long as all the sync operations sync on the same thing, whether it's the RAF or the FileChannel (given that there's a 1-to-1 relationship between the RAF and the FileChannel). For that matter you could also sync on some other object held in a static field, provided there's a 1-to-1 relationship wiht the RAF and FileChannel. Just make sure everyone is syncong on the same object, whciever one you choose.
Personally, just for readability I would sync on whichever object you're actually using more. In this case that's probably the FileChannel. But it doesn't really matter much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic