【原创】Java实现内存共享

内存共享就是对同一段内存的读写;用来进行进程之间的通信。

首先是写的代码:

package com.sharememory.test;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class WriteMemory {
String fileName = "shm.lock";
RandomAccessFile raFile;
FileChannel fc;
int iSize = 1024;
MappedByteBuffer mapBuf;
int iMode;

public WriteMemory() {
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}

public void init() throws Exception {
raFile = new RandomAccessFile(fileName, "rw");
fc = raFile.getChannel();
mapBuf = fc.map(FileChannel.MapMode.READ_WRITE, 0, iSize);
}

public void clearBuffer() {
// 清除文件内容
for (int i = 0; i < 1024; i++) {
mapBuf.put(i, (byte) 0);
}
}

public void putBuffer() throws Exception{
for (int i = 65; i < 91; i++) {
int index = i - 63;
int flag = mapBuf.get(0); // 可读标置第一个字节为 0
if (flag != 0) { // 不是可写标示 0,则重复循环,等待
i--;
continue;
}
mapBuf.put(0, (byte) 1); // 正在写数据,标志第一个字节为 1
mapBuf.put(1, (byte) (index)); // 写数据的位置

System.out.println("程序 WriteShareMemory:"
+ System.currentTimeMillis() + ":位置:" + index + " 写入数据:"
+ (char) i);

mapBuf.put(index, (byte) i);// index 位置写入数据
mapBuf.put(0, (byte) 2); // 置可读数据标志第一个字节为 2
Thread.sleep(513);
}
}

public boolean getLock() {
FileLock lock = null;
try {
lock = fc.tryLock();
} catch (IOException e) {
e.printStackTrace();
}
if (lock == null) {
return false;
} else {
return true;
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
WriteMemory map = new WriteMemory();
if (map.getLock()) {
try {
map.putBuffer();
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("can't get lock");
}
}
}




然后是读的代码

package com.sharememory.test;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class ReadMemory {
String fileName = "shm.lock";
RandomAccessFile raFile;
FileChannel fc;
int iSize = 1024;
MappedByteBuffer mapBuf;
int iMode;
int lastIndex = 0;

public ReadMemory() {
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}

public void init() throws Exception {
raFile = new RandomAccessFile(fileName, "rw");
fc = raFile.getChannel();
mapBuf = fc.map(FileChannel.MapMode.READ_WRITE, 0, iSize);
}

public void clearBuffer() {
// 清除文件内容
for (int i = 0; i < 1024; i++) {
mapBuf.put(i, (byte) 0);
}
}

public void getBuffer() throws Exception{
for(int i=1;i<27;i++){
int flag = mapBuf.get(0); //取读写数据的标志
int index = mapBuf.get(1); //读取数据的位置,2 为可读

if(flag != 2 || index == lastIndex){ //假如不可读,或未写入新数据时重复循环
i--;
continue;
}

lastIndex = index;
System.out.println("程序 ReadShareMemory:" + System.currentTimeMillis() +
":位置:" + index +" 读出数据:" + (char)mapBuf.get(index));

mapBuf.put(0,(byte)0); //置第一个字节为可读标志为 0

if(index == 27){ //读完数据后退出
break;
}
}
}

public boolean getLock() {
FileLock lock = null;
try {
lock = fc.tryLock();
} catch (IOException e) {
e.printStackTrace();
}
if (lock == null) {
return false;
} else {
return true;
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
ReadMemory map = new ReadMemory();
if (map.getLock()) {
try {
map.getBuffer();
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("can't get lock");
}
}
}


上面代码一个先运行的时候,会上锁(文件锁),另外其他的进程来访问的时候就访问不了,通过锁的机制来达到资源互斥和同步。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值