源代码如下:)jdk1.4.1下编译运行!import java.io.*;
import java.nio.channels.*;
import java.nio.MappedByteBuffer;public class ShareMemory {
public String fileName;
private RandomAccessFile lockFile;
private FileChannel fileChannel;
private FileLock flock = null;
private MappedByteBuffer mapBuf;
private int size;
private char mode;
/**
* 构造函数
* @return No
* @throws No
*/
public ShareMemory(String lockFile) {
this.fileName = lockFile;
try{
if(!this.isExist()){
this.lockFile = new RandomAccessFile(this.fileName,"rw");
this.lockFile.writeChar('n');
}else{
this.lockFile = new RandomAccessFile(this.fileName,"rw");
}
this.fileChannel = this.lockFile.getChannel();
this.size = (int)this.fileChannel.size();
this.mapBuf =
this.fileChannel.map(FileChannel.MapMode.READ_WRITE,0,size);
}catch(FileNotFoundException e){
System.out.println("Init ShareMemory failed");
return ;
}catch(Exception e){
System.out.println("Init ShareMemory failed");
return ;
}
}
private boolean isExist(){
File tmpFile = new File(this.fileName );
return tmpFile.exists();
}
public static synchronized void deleteShm(String fileName){
File tempFile = new File(fileName);
try{
if (tempFile.exists())
{
//tempFile.delete();
tempFile.deleteOnExit();
}
}catch(Exception e){
System.out.println("删除共享内存出错");
e.printStackTrace();
} } public synchronized char getMode(){
this.mode = this.mapBuf.getChar(0);
return this.mode;
}
public void setMode(char mode){
try{
this.flock = this.fileChannel.tryLock();
}
catch(Exception se){
System.err.println("共享内存系统故障!");
se.printStackTrace();
}
if(this.flock != null){
this.mapBuf.putChar(0,mode);
}
try{
this.flock.release();
}
catch(Exception se){
System.err.println("共享内存系统故障!");
se.printStackTrace();
}
}
public void finalize(){
try{
if(this.fileChannel != null)
this.fileChannel.close();
if(this.lockFile !=null)
this.lockFile.close();
this.deleteShm(this.fileName);
}catch(IOException ie){
System.err.println("关闭共享内存出错");
ie.printStackTrace();
}
} public static void main(String args[]){ ShareMemory shm = new ShareMemory(".$shm$");
if((args.length !=0)&&args[0].toLowerCase().equals("quit")){
if(shm.getMode()=='r')
{
shm.setMode('q');
System.out.println("|||结束批价程序|||");
return;
}else{
System.out.println("|||程序没有在运行|||");
return ;
}
}
if(shm.getMode()=='r')
{
System.out.println("|||批价程序已在运行中......|||");
return;
}else{
System.out.println("|||批价程序启动......|||");
shm.setMode('r');
}
System.out.println("1:Hello World Again & Again!");
while(shm.getMode() == 'r');
ShareMemory.deleteShm(shm.fileName);
System.out.println("2:Hello World Again & Again!");
}
}
import java.nio.channels.*;
import java.nio.MappedByteBuffer;public class ShareMemory {
public String fileName;
private RandomAccessFile lockFile;
private FileChannel fileChannel;
private FileLock flock = null;
private MappedByteBuffer mapBuf;
private int size;
private char mode;
/**
* 构造函数
* @return No
* @throws No
*/
public ShareMemory(String lockFile) {
this.fileName = lockFile;
try{
if(!this.isExist()){
this.lockFile = new RandomAccessFile(this.fileName,"rw");
this.lockFile.writeChar('n');
}else{
this.lockFile = new RandomAccessFile(this.fileName,"rw");
}
this.fileChannel = this.lockFile.getChannel();
this.size = (int)this.fileChannel.size();
this.mapBuf =
this.fileChannel.map(FileChannel.MapMode.READ_WRITE,0,size);
}catch(FileNotFoundException e){
System.out.println("Init ShareMemory failed");
return ;
}catch(Exception e){
System.out.println("Init ShareMemory failed");
return ;
}
}
private boolean isExist(){
File tmpFile = new File(this.fileName );
return tmpFile.exists();
}
public static synchronized void deleteShm(String fileName){
File tempFile = new File(fileName);
try{
if (tempFile.exists())
{
//tempFile.delete();
tempFile.deleteOnExit();
}
}catch(Exception e){
System.out.println("删除共享内存出错");
e.printStackTrace();
} } public synchronized char getMode(){
this.mode = this.mapBuf.getChar(0);
return this.mode;
}
public void setMode(char mode){
try{
this.flock = this.fileChannel.tryLock();
}
catch(Exception se){
System.err.println("共享内存系统故障!");
se.printStackTrace();
}
if(this.flock != null){
this.mapBuf.putChar(0,mode);
}
try{
this.flock.release();
}
catch(Exception se){
System.err.println("共享内存系统故障!");
se.printStackTrace();
}
}
public void finalize(){
try{
if(this.fileChannel != null)
this.fileChannel.close();
if(this.lockFile !=null)
this.lockFile.close();
this.deleteShm(this.fileName);
}catch(IOException ie){
System.err.println("关闭共享内存出错");
ie.printStackTrace();
}
} public static void main(String args[]){ ShareMemory shm = new ShareMemory(".$shm$");
if((args.length !=0)&&args[0].toLowerCase().equals("quit")){
if(shm.getMode()=='r')
{
shm.setMode('q');
System.out.println("|||结束批价程序|||");
return;
}else{
System.out.println("|||程序没有在运行|||");
return ;
}
}
if(shm.getMode()=='r')
{
System.out.println("|||批价程序已在运行中......|||");
return;
}else{
System.out.println("|||批价程序启动......|||");
shm.setMode('r');
}
System.out.println("1:Hello World Again & Again!");
while(shm.getMode() == 'r');
ShareMemory.deleteShm(shm.fileName);
System.out.println("2:Hello World Again & Again!");
}
}
新代码如下:希望对感兴趣的朋友有所帮助。/**
* <p>Title: ibios批价</p>
* <p>Description: 共享内存,设置进程文件锁</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: CTC</p>
* @author zhengyao gaosipeng
* @version 1.0
*/import java.io.*;
import java.nio.channels.*;
import java.nio.MappedByteBuffer;public class ShareMemory {
public String fileName;
private RandomAccessFile lockFile;
private FileChannel fileChannel;
private FileLock flock = null;
private FileLock tmpFirst = null;
private MappedByteBuffer mapBuf;
private int size;
private char mode;
/**构造函数
* @param lockFile 共享内存名
* @return No
* @throws No
*/
public ShareMemory(String lockFile) {
this.fileName = lockFile;
try{
if(this.isFirstRun()){
this.lockFile = new RandomAccessFile(this.fileName,"rw");
this.lockFile.writeChar('n');
}else{
this.lockFile = new RandomAccessFile(this.fileName,"rw");
}
this.fileChannel = this.lockFile.getChannel();
this.size = (int)this.fileChannel.size();
this.mapBuf =
this.fileChannel.map(FileChannel.MapMode.READ_WRITE,0,size);
}catch(FileNotFoundException e){
System.out.println("Init ShareMemory failed");
return ;
}catch(Exception e){
System.out.println("Init ShareMemory failed");
return ;
}
}
private boolean isFirstRun(){
boolean rtValue = false;
try{
RandomAccessFile tmpRAF = new RandomAccessFile("$#@$","rw");
FileChannel tmpFileChannel = tmpRAF.getChannel();
this.tmpFirst = tmpFileChannel.tryLock();
}catch(Exception e){
System.err.println("共享内存系统故障!");
e.printStackTrace();
}
if(this.tmpFirst != null)
rtValue = true;
return rtValue;
} public synchronized char getMode(){
this.mode = this.mapBuf.getChar(0);
return this.mode;
}
public void setMode(char mode){
try{
this.flock = this.fileChannel.tryLock();
}
catch(Exception se){
System.err.println("共享内存系统故障!");
se.printStackTrace();
}
if(this.flock != null){
this.mapBuf.putChar(0,mode);
}
try{
if(this.flock != null)
this.flock.release();
}
catch(Exception se){
System.err.println("共享内存系统故障!");
se.printStackTrace();
}
}
public void finalize(){
try{
if(this.fileChannel != null)
this.fileChannel.close();
if(this.lockFile !=null)
this.lockFile.close();
}catch(IOException ie){
System.err.println("关闭共享内存出错");
ie.printStackTrace();
}
} public static void main(String args[]){ ShareMemory shm = new ShareMemory(".$shm$");
if((args.length !=0)&&args[0].toLowerCase().equals("quit")){
if(shm.getMode()=='r')
{
shm.setMode('q');
System.out.println("|||结束批价程序|||");
return;
}else{
System.out.println("|||程序没有在运行|||");
return ;
}
}
if(shm.getMode()=='r')
{
System.out.println("|||批价程序已在运行中......|||");
return;
}else{
System.out.println("|||批价程序启动......|||");
shm.setMode('r');
}
System.out.println("1:Hello World Again & Again!");
while(shm.getMode() == 'r');
System.out.println("2:Hello World Again & Again!");
}
}