Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Inter Process communication and Inter Thread communication?

What is the difference between a Thread and a Process in the Java context? How is inter-Process communication and inter-Thread communication achieved in Java? Please point me at some real life examples.

like image 526
JavaUser Avatar asked Mar 27 '10 01:03

JavaUser


People also ask

What are the differences between inter-thread communication in Java and inter-process communication in OS?

The fundamental difference is that threads live in the same address spaces, but processes live in the different address spaces. This means that inter-thread communication is about passing references to objects, and changing shared objects, but processes is about passing serialized copies of objects.

What is inter-thread communication in Java?

Inter-thread communication in Java is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed. Note: Inter-thread communication is also known as Cooperation in Java.

What are the two methods used for inter-thread communication?

InterThread Communication is the process in which two threads communicate with each other by using wait (), notify (), and notifyAll () methods. The Thread which is required updation has to call the wait() method on the required object then immediately the Thread will be entered into a waiting state.


1 Answers

The fundamental difference is that threads live in the same address spaces, but processes live in the different address spaces. This means that inter-thread communication is about passing references to objects, and changing shared objects, but processes is about passing serialized copies of objects.

In practice, Java interthread communication can be implemented as plain Java method calls on shared object with appropriate synchronization thrown in. Alternatively, you can use the new concurrency classes to hide some of the nitty-gritty (and error prone) synchronization issues.

By contrast, Java interprocess communication is based at the lowest level on turning state, requests, etc into sequences of bytes that can be sent as messages or as a stream to another Java process. You can do this work yourself, or you can use a variety of "middleware" technologies of various levels of complexity to abstract away the implementation details. Technologies that may be used include, Java object serialization, XML, JSON, RMI, CORBA, SOAP / "web services", message queing, and so on.

At a practical level, interthread communication is many orders of magnitude faster than interprocess communication, and allows you to do many things a lot more simply. But the downside is that everything has to live in the same JVM, so there are potential scalability issues, security issues, robustness issues and so on.

like image 60
Stephen C Avatar answered Nov 09 '22 23:11

Stephen C