views:

308

answers:

5

I have been reading the descriptions of referencing in Java, and, while I feel I understand them, I am not sure if Java allows me to do the following: we have two threads in the same JVM communicating via sockets, and we would like to pass what is essentially the address of a large chunk of data across the socket, without copying the data itself. The solution may be quite obvious to the initiated, but I can't figure it out! Help would be appreciated.

A: 

It may be possible using JNI, but I don't think it's possible with pure Java.

Given that you're within the same JVM (and that's the only chance you've got of making it work, unless you use a memory mapped file) do you really have to use sockets? Can't you create a more "direct" API?

Jon Skeet
A: 

There are two way you could do this.

  1. put the reference into a global location which can be retrieved later. Send an object over the socket which when de serialized is resolved to the globally accessing reference. (Only works in the same JVM)
  2. Make the large object accessible via RMI. Send an object which when de serialized creates an RMI proxy to your large object.

Using a direct API which be more efficient and simpler. I suggest considering a BlockingQueue.

Peter Lawrey
+1  A: 

AFAIK Java does not provide the means to get the memory reference of an object. What might work for you though is to use a shared array or shared vector into which you place the data as an object into the vector/array and pass the index of the object over the socket.

marcog
A: 

If you are passing the data between 2 threads of the same program you can simply pass a reference to the area via any channel from the java.concurrent package.

If you are are running 2 separate programs then you must serialize the data (if you really want the data itself to be passed).

If you only want access to the data you can publish a service that performs the desired transformation via RMI.

MahdeTo
+1  A: 

What else travels on this socket? If it is your own protocol then add a verb to the protocol that says "get that big thing from a known location" and just send the verb. How many of these objects are there? Can you make a map - string, object - and just pass the string over the socket?

jmucchiello
Actually I like that! Thanks! It reminds me of something I did in another environment some years ago - wonder why I didn't think of it :-)
Paul Morrison