跨进程同步锁的实现方法:
1. C++ named mutex
2. 文件是全系统通用的,可以通过文件锁来实现变相的跨进程锁
一个 java 例子:
ProgramA
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; public class ProgramA { public static void main(String[] args) throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile("884e94ae-e524-4c73-9200-3df9bec9f6af", "rw"); FileChannel fileChannel = randomAccessFile.getChannel(); FileLock fileLock = null; try { fileLock = fileChannel.lock(); System.out.println("A已获取锁"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); reader.readLine(); } finally { fileLock.release(); System.out.println("A已释放锁"); } } }
ProgramB:
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; public class ProgramB { public static void main(String[] args) throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile("884e94ae-e524-4c73-9200-3df9bec9f6af", "rw"); FileChannel fileChannel = randomAccessFile.getChannel(); FileLock fileLock = null; try { System.out.println("B正在获取锁"); fileLock = fileChannel.lock(); System.out.println("B已获取锁"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); reader.readLine(); } finally { fileLock.release(); System.out.println("B已释放锁"); } } }
其实还可以这样实现:
import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; public class CrossProcessLocker implements AutoCloseable { private String id = null; private FileLock fileLock = null; @SuppressWarnings("resource") public CrossProcessLocker(String id) throws IOException { this.id = id; RandomAccessFile randomAccessFile = new RandomAccessFile(id, "rw"); FileChannel fileChannel = randomAccessFile.getChannel(); fileLock = fileChannel.lock(); } @Override public void close() throws Exception { fileLock.release(); } }
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; public class ProgramA { public static void main(String[] args) throws Exception { System.out.println("A正在获取锁"); try(CrossProcessLocker cpl = new CrossProcessLocker("884e94ae-e524-4c73-9200-3df9bec9f6af")) { System.out.println("A已获取锁"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); reader.readLine(); } System.out.println("A已释放锁"); } }
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.util.Timer; import java.util.TimerTask; public class ProgramB { public static void main(String[] args) throws Exception { System.out.println("B正在获取锁"); try(CrossProcessLocker cpl = new CrossProcessLocker("884e94ae-e524-4c73-9200-3df9bec9f6af")) { System.out.println("B已获取锁"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); reader.readLine(); } System.out.println("B已释放锁"); } }
桂棹兮兰桨,击空明兮溯流光。