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
import java.lang.management.ManagementFactory;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import static java.nio.file.StandardOpenOption.*;
public class Main {
public static void main(String args[]) throws Exception {
final Path lockPath =
FileSystems.getDefault().getPath(".lock");
final String processID =
ManagementFactory.getRuntimeMXBean().getName();
try (final FileLock lock =
FileChannel.open(lockPath, WRITE, CREATE).lock()) {
System.out.println(processID + " acquired the lock");
Thread.sleep(1000); // simulates other work being done
System.out.println(processID + " is releasing the lock");
}
}
}
Enter to Rename, Shift+Enter to Preview
1
java Main& java Main& java Main&
Enter to Rename, Shift+Enter to Preview