How to do Inter process communication in Java? Example Tutorial

Hello guys, in the past, I have shown you how to do inter-thread communication in Java using wait-notify, and today, I will teach you how to do inter-process communication in Java. There are many ways to do inter-process communication Java, you can use Sockets, both TCP and UDP, you can use RMI, you can use web services, or you can use the memory-mapped file. The socket is the most common way of achieving inter-process communication if two processes are in two different hosts and connected via a network. RMI and WebService can also be used for similar purposes, but the last one, inter-process communication using memory-mapped files, is particularly useful if you are communicating with other processes in the same host, sharing the same memory and file system.

You can use a memory-mapped file to talk to another Java process or C/C++ process because they share the same memory.

It is also the fastest way to share data between two processes, more rapid than using loopback sockets.

In a multi-core system, two processes can run on separate cores, and if you are really doing fast data sharing, you also need a mechanism to tell JVM that it's time to flush CPU cache, using a static volatile variable is an excellent way to say that, alternatively you can use Thread.sleep() to give some time to JVM to update CPU cache.

Btw, if you are serious about building a high-performance Java application then I also recommend you check out the Java Multithreading, Concurrency, and Performance Optimization course which is a great course for experienced Java programmers. You will learn tips and tricks to create highly concurrent and fast Java applications.




How to do Interprocess communication in Java

Here is a code example of two processes communicating with each other using a Memory Mapped file in Java. 


Process 1 which is producing data :


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

/**
 * Inter process communication in Java using memory mapped file
 *
 * @author WINDOWS 8
 */

public class Producer {

    public static void main(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);

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
     
        for(int i=1; i            mem.put( (byte) i);
            System.out.println("Process 1 : " + (byte)i );
            Thread.sleep(1); // time to allow CPU cache refreshed
        }
    }
}

Output
Process 1: 1
Process 1: 2
Process 1 : 3
Process 1: 4
Process 1: 5
Process 1: 6
Process 1: 7
Process 1: 8
Process 1: 9


And here is a diagram which shows how this whole thing is working together:

How to do Inter process communication in Java? Example Tutorial


Process 2, which is consuming data


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

/**
 * Inter process communication in Java using memory mapped file
 *
 * @author WINDOWS 8
 */

public class Consumer {

    public static void main(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);
     
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
     
     
        int value = mem.get();
        while(value != 0){
            System.out.println("Process 2 : " + value);
            value = mem.get();
        }
    }
}


Output
Process 2: 1
Process 2: 2
Process 2 : 3
Process 2: 4
Process 2: 5
Process 2: 6
Process 2: 7
Process 2: 8
Process 2: 9


That's all about how to do inter-process communication in Java. In this article, you have learned how to use the Memory-mapped file for inter-process communication in Java.


Further Learning
Multithreading and Parallel Computing in Java
Java Concurrency in Practice - The Book
Applying Concurrency and Multi-threading to Common Java Patterns
Java Concurrency in Practice Course by Heinz Kabutz
Java Multithreading, Concurrency, and Performance Optimization

Other Java Multithreading and Concurrency Articles you may like
  • The 2020 Java Developer RoadMap (roadmap)
  • Top 5 Books to Master Concurrency in Java (books)
  • Difference between volatile, synchronized, and atomic variable in Java (answer)
  • 10 Java Multithreading and Concurrency Best Practices (article)
  • Top 50 Multithreading and Concurrency Questions in Java (questions)
  • 10 Courses to learn Java in 2020 (courses)
  • Difference between CyclicBarrier and CountDownLatch in Java? (answer)
  • How to avoid deadlock in Java? (answer)
  • Difference between Executor and ExecutorService in Java? (answer)
  • Understanding the flow of data and code in Java program (answer)
  • Is Java Concurrency in Practice still valid in 2020 (answer)
  • How to do inter-thread communication in Java using wait-notify? (answer)
  • 10 Tips to become a better Java Developer in 2020 (tips)
  • Difference between ForkJoinPool and Executor Framework in Java(answer)
  • 5 Essential Skills to Crack Java Interviews (skills)
  • What is Happens Before in Java Concurrency? (answer)
  • 5 Courses to Learn Java Multithreading in-depth (courses)

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are serious to improve your Java Multithreading and Concurrency Skills but looking for a free course to start with then I also suggest you check out this awesome free Java Multithreading course on Udemy. It's completely free and more 100K Java programmers already benefited from it. All you need to do is create a free Udemy account and enroll in that course. 

No comments:

Post a Comment