-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileLockDemo.java
More file actions
76 lines (68 loc) · 2.02 KB
/
Copy pathFileLockDemo.java
File metadata and controls
76 lines (68 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.io.IOException;
import java.io.RandomAccessFile;
public class FileLockDemo{
private static final int MAX_QUERIES = 150000;
private static final int MAX_UPDATES = 150000;
private static final int REC_LENGTH = 16;
static ByteBuffer buffer = ByteBuffer.allocate(REC_LENGTH);
static IntBuffer intBuffer = buffer.asIntBuffer();
static int counter = 1;
public static void main(String[] args) throws IOException{
boolean isWriter = false;
if(args.length != 0){
isWriter = true;
}
RandomAccessFile raf = new RandomAccessFile("FileLockDemo.temp", isWriter ? "rw" : "r");
FileChannel fc = raf.getChannel();
if(isWriter){
update(fc);
}else{
query(fc);
}
}
static void query(FileChannel fc) throws IOException{
for(int i=0; i<MAX_QUERIES; i++){
System.out.println("acquiring shared lock");
FileLock lock = fc.lock(0, REC_LENGTH, true); // Lock a record region of the file.
try{
buffer.clear();
fc.read(buffer, 0); // From file position 0.
int a = intBuffer.get(0);
int b = intBuffer.get(1);
int c = intBuffer.get(2);
int d = intBuffer.get(3);
System.out.println("Reading: " + a + ", " + b + ", " + c + ", " +d);
if(a*2!=b || a*3!=c || a*4!=d){
System.err.println("Error: record corruption.");
return;
}
}finally{
lock.release();
}
}
}
static void update(FileChannel fc) throws IOException{
for(int i=0;i<MAX_UPDATES;i++){
System.out.println("acquiring exclusive lock.");
FileLock lock = fc.lock(0, REC_LENGTH, false);
try{
intBuffer.clear();
int a = counter;
int b = counter*2;
int c = counter*3;
int d = counter*4;
System.out.println("Writing: " + a + ", " + b + ", " + c + ", " + d);
intBuffer.put(a).put(b).put(c).put(d);
counter++;
buffer.rewind();
fc.write(buffer, 0); // write into file beginning from position 0.
}finally{
lock.release();
}
}
}
}