先看看传统的进程间通信的手段:
(1) 管道(PIPE)
(2) 命名管道(FIFO)
(3) 信号灯(Semphore)
(4) 消息队列(MessageQueue)
(5) 共享内存(SharedMemory)
(6) Socket(当然也有Socket)
如果加上上面提到的临时文件(临时文件其实是很难处理的,不同的进程间单靠临时文件可以交互信息,但是做到进程的调度控制确是很费力的事情,当然也不是不能做到)
把JAVA进程理解为JVM进程。很明显,传统的这些大部分技术是无法被俺们的应用程序利用了(这些进程间通信都是靠系统调用来实现的)。但是JAVA也有很多方法可以进行进程间通信的。
除了上面提到的Socket之外,当然首选的IPC可以使用RMI,或者CORBA也可以。
其实JAVA的CORBA实现也是通过RMI来实现的,而RMI归根结底也是靠Socket来实现的。
所以说JAVA进程间通信的最基本手段是Socket也不为过。
package sharedmemory;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Consumer extends Thread
{
private String mFileName;
private FileChannel mFileChannel;
private MappedByteBuffer mMappedByteBuffer;
public Consumer(String fn)
{
try
{
mFileName=fn;
// 获得一个可读写的随机存取文件对象
RandomAccessFile RAFile=new RandomAccessFile(mFileName,"r");
// 获得相应的文件通道
mFileChannel=RAFile.getChannel();
// 取得文件的实际大小,以便映像到共享内存
int size=(int)mFileChannel.size();
// 获得共享内存缓冲区,该共享内存可读
mMappedByteBuffer=mFileChannel.map(FileChannel.MapMode.READ_ONLY,0,size).load();
}
catch(IOException ex)
{
System.out.println(ex);
}
}
public void run()
{
while(true)
{
try
{
Thread.sleep(300);
FileLock lock=null;
lock=mFileChannel.tryLock(0,10,true);
if(lock==null)
{
System.err.println("Consumer: lock failed");
continue;
}
Thread.sleep(200);
System.out.println("Consumer: "+mMappedByteBuffer.getInt(0)+":"+mMappedByteBuffer.getInt(4)+":"+mMappedByteBuffer.getInt(8));
lock.release();
}
catch(IOException ex)
{
System.out.print(ex);
}
catch(InterruptedException ex)
{
System.out.print(ex);
}
}
}
public static void main(String args[])
{
Consumer consumer=new Consumer("sharedMemory.bin");
consumer.start();
}
}
package sharedmemory;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Producer extends Thread
{
private String mFileName;
private FileChannel mFileChannel;
private MappedByteBuffer mMappedByteBuffer;
public Producer(String fn)
{
try
{
mFileName=fn;
// 获得一个可读写的随机存取文件对象
RandomAccessFile RAFile=new RandomAccessFile(mFileName,"rw");
// 获得相应的文件通道
mFileChannel=RAFile.getChannel();
// 取得文件的实际大小,以便映像到共享内存
int size=(int)mFileChannel.size();
// 获得共享内存缓冲区,该共享内存可读
mMappedByteBuffer=mFileChannel.map(FileChannel.MapMode.READ_WRITE,0,size).load();
}
catch(IOException ex)
{
System.out.println(ex);
}
}
public void run()
{
int i=0;
while(true)
{
try
{
FileLock lock=null;
lock=mFileChannel.tryLock();
if(lock==null)
{
System.err.println("Producer: lock failed");
continue;
}
mMappedByteBuffer.putInt(0,++i);
mMappedByteBuffer.putInt(4,++i);
mMappedByteBuffer.putInt(8,++i);
System.out.println("Producer: "+(i-3)+":"+(i-2)+":"+(i-1));
Thread.sleep(200);
lock.release();
Thread.sleep(500);
}
catch(IOException ex)
{
System.out.print(ex);
}
catch(InterruptedException ex)
{
System.out.print(ex);
}
}
}
public static void main(String args[])
{
Producer producer=new Producer("sharedMemory.bin");
producer.start();
}
}