*Character

---scatter/gather

---always blocking

---attempts to use native I/O services when possible

---ThreadSafe,but Operations that affect the channel's position or the file size are single-threaded

 

*API

---truncate(long size)

if size<CurrentSize then discarding any bytes beyond the new end of the file

if size>=CurrentSize then the file is not modified.

if position>size ,then position=size

---force(boolean metaData)

将缓存内容同步到磁盘

metaData的含义,true表示将file的meta更新信息也保存到文件,false表示只更新文件的内容.

这个方法将引起可能的IO操作.

---FileChannel.position(long newPosition)

设置读取或修改的位置,注意FileChannel的position和文件流的position是同步的.

FileChannel测试

*文件锁FileLock

---用于进程间并发

Locks are associated with files, not channels. Use locks to coordinate
with external processes, not between threads in the same JVM.

---概念

共享锁:共享读操作,但只能一个写

排它锁:读写一时一个.

---FileLock FileChannel.lock(long position, long size, boolean shared) ,文件锁

shared的含义:是否使用共享锁,一些不支持共享锁的操作系统,将自动将共享锁改成排它锁.

---lock()和tryLock()的区别

lock()阻塞的方法

tryLock()非阻塞,当未获得锁时,返回null.

---FileLock的生命周期

在调用FileLock.release(),或者Channel.close(),或者JVM关闭

---FileLock是线程安全的

---boolean java.nio.channels.FileLock.overlaps(long position, long size)

true表示当前锁在区域内,false表示当前锁的区域与参数区域不重叠

posted on 2011-03-16 21:20  戴忠  阅读(1186)  评论(0编辑  收藏  举报