趣文网 > 作文大全

程序员:Java文件锁定 解锁和其它NIO操作

2020-12-01 21:55:01
相关推荐

文件锁

java中i/o的文件锁定有两种:一种是独占锁,一种是共享锁。

共享锁既是共享读操作,但是只有一个可以进行写操作,共享锁防止其他正在运行的程序获取重复的独占锁,但是允许其他程序可以获取共享锁。

独占锁既只允许程序获取一个锁,独占锁防止程序获取其他的任何锁。

样例

样例基于Java的IO和NIO编写,分别对这个RandomAccessFile对象生成的FileChannel对象加锁/释放锁。

为保证锁的释放,需要finally块中进行处理。

前面有建立ByteBuffer,后面则是将ByteBuffer对象转成CharBuffer对象从而在控制台打印出来~~

详细说明见代码,大多数注释都是用嘤文加的~~

import java.io.*;

import java.nio.*;

import java.nio.channels.*;

import java.nio.charset.*;

public class LockingFileExample {

//排它/互斥

public static final boolean EXCLUSIVE = false;

//共享

public static final boolean SHARED = true;

//文件路径

private static final String FILE_PATH = "src/com/neu/algorithm/os_file/file.txt";

public static void main(String[] args) throws IOException {

//共享锁

FileLock sharedLock = null;

//排它锁/互斥锁

FileLock exclusiveLock = null;

try {

RandomAccessFile raf = new RandomAccessFile(FILE_PATH, "rw");

//get the channel from the file

FileChannel ch = raf.getChannel();

ByteBuffer buffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());

//this locks the first half of the file - exclusive

exclusiveLock = ch.lock(0, raf.length()/2, EXCLUSIVE);

/* Now modify the data . . . */

ch.position(raf.length());

ch.write(buffer);

//release the lock

exclusiveLock.release();

//this locks the second half of the file - shared

sharedLock = ch.lock(raf.length()/2+1, raf.length(), SHARED);

/* Now read the data . . . */

ByteBuffer buffer2 = ch.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());

ch.read(buffer2);

//release the lock

sharedLock.release();

buffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());

//create the UTF-8 decoder

Charset charset = Charset.forName("UTF-8");

CharsetDecoder charsetDecoder = charset.newDecoder();

//ByteBuffer -> CharBuffer

CharBuffer charBuffer = charsetDecoder.decode(buffer);

//output

System.out.println(charBuffer);

} catch (IOException ioe) {

ioe.printStackTrace();

} finally {

if (exclusiveLock != null) {

exclusiveLock.release();

}

if (sharedLock != null) {

sharedLock.release();

}

}

}

}

文件内容

Loving him is like driving a new Maserati down a dead end street

Faster than the wind, passionate as sin, ending so suddenly

Loving him is like trying to change your mind once you’re already flying through the free fall

Like the colors in autumn, so bright, just before they lose it all

Losing him was blue, like I’ve never known

Missing him was dark grey, all alone

Forgetting him was like trying to know somebody you never met

But loving him was red

Loving him was red

Touching him was like realizing all you ever wanted was right there in front of you

Memorizing him was as easy as knowing all the words to your old favorite song

Fighting with him was like trying to solve a crossword and realizing there’s no right answer

Regretting him was like wishing you’d never found out that love could be that strong

Losing him was blue, like I’ve never known

Missing him was dark grey, all alone

Forgetting him was like trying to know somebody you never met

But loving him was red

Oh, red burning red

Remembering him comes in flashbacks, in echoes

Tell myself it’s time now, gotta let go

But moving on from him is impossible when I still see it all in my head

Burning red

Loving him was red

Losing him was blue, like I’ve never known

Missing him was dark grey, all alone

Forgetting him was like trying to know somebody you never met

‘Cause loving him was red

Yeah, yeah red

burning red

And that’s why he’s spinning round in my head

Comes back to me, burning red

Yeah, yeah

His love was like driving a new Maserati down a dead end street

运行效果

Console打印文件内容:

文件

阅读剩余内容
网友评论
相关内容
延伸阅读
小编推荐

大家都在看

运动会作文400字 感恩的作文 假如没有网络作文 写一篇游记作文400字 扬起理想的风帆作文600字 我最喜欢的玩具汽车作文 我学会了做饭600字作文 童年趣事作文四年级 操场上作文 芦荟 作文 散步的作文300字 自然风景作文400字 写同学的作文800字 的颜色作文 辛苦的暑假作文400字 师生情满分作文 景物描写小作文 写人品质的作文 诚信最珍贵作文400字 按照空间顺序写的作文 国家利益高于一切作文 作文框架 关于母亲的作文500字 月考后作文 运动会的作文结尾 小学生作文网课 调查报告作文 有你同行作文600字 夏天的荷花池作文 历经磨难作文