devxlogo

Object passing between threads

Object passing between threads

Question:
Am implementing a network simulation program thatrequires a sender and receiver thread, with a frame object being passed between the two threads.Could you suggest a way of connecting the threads by means of an object stream that would allow the object to be passed between the two threads.What is required is much like the pipe implementation in c except from what I gather in java the data types that can be sent are limited.

Answer:
If you feel the best way to implement yoursimulation is by using streams, you will wantto look at java.io.PipedInputStream andjava.io.PipedOutputStream. These allow you toread and write data between two threads. Allyou have to do is wrap them with ObjectInputStream and ObjectOutputStream andalso make your network frame object implementthe Serializable interface, so that it maybe written to an ObjectInputStream.

There may be alternative ways to implementyour simulation by using explicit synchronizationand variable assignments rather than usingstreams to simulate the sending and receivingof frames.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist