You may use a memory-mapped file to speak to a different Java course of or C/C++ course of as a result of they share the identical reminiscence. It’s also the quickest solution to share knowledge between two processes, extra fast than utilizing loopback sockets.
In a multi-core system, two processes can run on separate cores, and if you’re actually doing quick knowledge sharing, you additionally want a mechanism to inform JVM that it is time to flush CPU cache, utilizing a static risky variable is a wonderful solution to say that, alternatively you should utilize Thread.sleep() to offer a while to JVM to replace CPU cache.
Btw, if you’re critical about constructing a high-performance Java utility then I additionally advocate you try these Java Multithreading and Concurrency programs which is a superb course for skilled Java programmers. You’ll be taught suggestions and tips to create extremely concurrent and quick Java purposes.
How you can do Interprocess communication in Java? Instance
Here’s a code instance of two processes speaking with one another utilizing a Reminiscence Mapped file in Java.
Course of 1 which is producing knowledge :
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Producer {
public static void essential(String args[]) throws IOException, InterruptedException {
RandomAccessFile rd = new RandomAccessFile("C:/temp/mapped.txt", "rw");
FileChannel fc = rd.getChannel();
MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_WRITE, 0, 1000);
strive {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=1; i mem.put( (byte) i);
System.out.println("Course of 1 : " + (byte)i );
Thread.sleep(1);
}
}
}
Output
Course of 1: 1
Course of 1: 2
Course of 1 : 3
Course of 1: 4
Course of 1: 5
Course of 1: 6
Course of 1: 7
Course of 1: 8
Course of 1: 9
And here’s a diagram that reveals how this entire factor is working collectively:
Course of 2, which is consuming knowledge
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Client {
public static void essential(String args[]) throws IOException {
RandomAccessFile rd = new RandomAccessFile("C:/temp/mapped.txt", "r");
FileChannel fc = rd.getChannel();
MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_ONLY, 0, 1000);
strive {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int worth = mem.get();
whereas(worth != 0){
System.out.println("Course of 2 : " + worth);
worth = mem.get();
}
}
}
Output
Course of 2: 1
Course of 2: 2
Course of 2 : 3
Course of 2: 4
Course of 2: 5
Course of 2: 6
Course of 2: 7
Course of 2: 8
Course of 2: 9
That is all about methods to do inter-process communication in Java. On this article, you’ve gotten discovered methods to use a Reminiscence-mapped file for inter-process communication in Java. This program not solely teaches you methods to do inter-process communication but in addition methods to use the memory-mapped recordsdata in Java which can be utilized for quick learn and write operations. Many Java programmers additionally use that as a cache in Java.
Thanks for studying this text thus far. In the event you like this text then please share it with your folks and colleagues. If in case you have any questions or suggestions then please drop a be aware.
P. S. – In case you are critical to enhance your Java Multithreading and Concurrency Abilities however in search of a free course to start out with then I additionally recommend you try this superior free Java Multithreading course on Udemy. It is fully free and greater than 100K Java programmers already benefited from it. All it’s worthwhile to do is create a free Udemy account and enroll in that course.