Sunday, May 5, 2024
HomeJavaHow you can do Inter course of communication in Java? Instance Tutorial

How you can do Inter course of communication in Java? Instance Tutorial


Whats up guys, prior to now, I’ve proven you how to do inter-thread communication in Java utilizing wait-notify, and in the present day, I’ll train you methods to do inter-process communication in Java. There are lots of methods to do inter-process communication in Java, you should utilize Sockets, each TCP and UDP, you should utilize RMI (Distant Methodology Invocation), you should utilize internet companies, or you should utilize memory-mapped file. The socket is the commonest manner of reaching inter-process communication if two processes are in two totally different hosts and linked by way of a community. RMI and WebService will also be used for related functions, however the final one, inter-process communication utilizing memory-mapped recordsdata, is especially helpful if you’re speaking with different processes in the identical host, sharing the identical reminiscence and file system.

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:

How to do Inter process communication in Java? Example Tutorial

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. 

Different Java Multithreading and Concurrency Articles chances are you’ll like

  • The Full Java Developer RoadMap (roadmap)
  • Prime 5 Books to Grasp Concurrency in Java (books)
  • Distinction between risky, synchronized, and atomic variable in Java (reply)
  • 10 Java Multithreading and Concurrency Greatest Practices (article)
  • Prime 50 Multithreading and Concurrency Questions in Java (questions)
  • 10 Programs to be taught Java for Newbies (programs)
  • Distinction between CyclicBarrier and CountDownLatch in Java? (reply)
  • How you can keep away from impasse in Java? (reply)
  • Distinction between Executor and ExecutorService in Java? (reply)
  • Understanding the circulation of information and code in Java program (reply)
  • Is Java Concurrency in Observe nonetheless legitimate  (reply)
  • How you can do inter-thread communication in Java utilizing wait-notify? (reply)
  • 10 Tricks to turn into a greater Java Developer (suggestions)
  • Distinction between ForkJoinPool and Executor Framework in Java(reply)
  • 5 Important Abilities to Crack Java Interviews (expertise)
  • What’s Occurs Earlier than in Java Concurrency? (reply)
  • 5 Programs to Be taught Java Multithreading in-depth (programs)

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. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments