-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockTest.java
68 lines (56 loc) · 1.64 KB
/
LockTest.java
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
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
class LockTest {
private static final int DEFAULT_DELAY = 10;
static class Configuration {
public File file;
public int delay;
}
public static void main(String[] args) throws Exception {
Configuration config = configuration(args);
if (config == null) {
System.exit(1);
}
RandomAccessFile file = new RandomAccessFile(config.file, "rw");
FileChannel channel = file.getChannel();
FileLock lock = channel.tryLock(0L, Long.MAX_VALUE, true);
if (lock == null) {
System.out.println("waiting for lock");
lock = channel.lock(0L, Long.MAX_VALUE, true);
}
System.out.println("lock acquired");
System.out.format("sleeping for %d seconds\n", config.delay);
Thread.sleep(1000 * config.delay);
lock.release();
System.out.println("lock released");
file.close();
}
public static Configuration configuration(String[] args) {
if (args.length < 1 || args.length > 2) {
showUsage();
return null;
}
Configuration config = new Configuration();
config.file = new File(args[0]);
config.delay = DEFAULT_DELAY;
if (args.length == 2) {
try {
config.delay = Integer.parseInt(args[1]);
if (config.delay == 0) {
showUsage();
return null;
}
}
catch (NumberFormatException ex) {
showUsage();
return null;
}
}
return config;
}
public static void showUsage() {
System.err.println("usage: java LockTest filename [duration]");
}
}