3

assume we have 2 threads, thread A and thread B.

thread A is the main method and contain large data structures.

is it possible to create a second thread and pass the address(a pointer) of the data structure (local to thread A) to thread B so both thread can read from the data structure?

the point of this is to avoid the need to duplicate the entire data structure on thread B or spend a lot of time pulling relevant information from the data structure for thread B to use

keep in mind that neither thread is modifying the data

mellamokb
  • 56,094
  • 12
  • 110
  • 136
Jdak
  • 33
  • 3

5 Answers5

3
  1. In Java, the term pointer is not used, but reference.
  2. It is possible to pass it, as any other object, to another thread. As any (non-final) class in Java, you can extend it, add members, add constructors etc.
  3. (If you need to modify the data) You need to make sure that there are no concurrency issues.
MByD
  • 135,866
  • 28
  • 264
  • 277
1

It's known as a reference in java, as you don't have access directly to a pointer in a conventional sense. (For most cases it's "safe" to think of it as every reference is a pointer that is always passed by value and the only legal operation is to dereference it. It is NOT the same as a C++ 'reference.')

You can certainly share references among threads. Anything that's on the heap can be seen and used by any thread that can get a reference to it. You can either put it in a static location, or set the value of a reference on your Runnable to point to the data.

public class SharedDataTest {

  private static class SomeWork implements Runnable {
    private Map<String, String> dataTable;

    public SomeWork(Map<String, String> dataTable) {
      this.dataTable = dataTable;
    }

    @Override
    public void run() {
      //do some stuff with dataTable
    }
  }

  public static void main(String[] args) {

    Map<String, String> dataTable = new ConcurrentHashMap<String, String>();

    Runnable work1 = new SomeWork(dataTable);
    Runnable work2 = new SomeWork(dataTable);

    new Thread(work1).start();
    new Thread(work2).start();

  }

}
Affe
  • 47,174
  • 11
  • 83
  • 83
  • i did not expect anyone to write code for this. thanks for all the great answer everyone. – Jdak Apr 20 '12 at 20:39
1

Yes it is possible and is a usual thing to do but you need to make sure that you use proper synchronization to ensure that both threads see an up to date version of the data.

assylias
  • 321,522
  • 82
  • 660
  • 783
0

It is safe to share a reference to immutable object. Roughly speaking, immutable object is the object that doesn't change its state after construction. Semantically immutable object should contain only final fields which in turn reference immutable objects.

If you want to share reference to mutable object you need to use proper synchronization, for example by using synchronized or volatile keywords.

Easy way to share data safely would be to use utilities from java.util.concurrent package such as AtomicReference or ConcurrentHashMap, however you still have to be very careful if objects you share are mutable.

hgrey
  • 3,033
  • 17
  • 21
0

If you are not doing any modification in the shared data you can have a shared reference and there will be no significant overhead.

Be careful however when you start modifying the shared object concurrently, in this case you can use the data structures provided in java (see for instance factory methods in Collections), or use a custom synchronisation scheme, for instance with java.util.concurrent.locks.ReentrantLock.

Victor P.
  • 630
  • 1
  • 6
  • 14