简体   繁体   中英

Java: multithreading on massive data: sharing data between threads?

I want to run a multithreaded program on massive data. I usually create a class which is callable (or runnable) and pass the data needed for the process to the class.

public class CallableTrainer implements Callable<PredictorResult> {

   dataType data;

   CallableTrainer( dataType massiveData ) { 
       this.data = massiveData;
   }

   @Override
   public PredictorResult call() throws Exception {
        // do something and return ... 
   }
}

Based on the above implementation, I assume that the 'massiveData' is always copied for each thread (right?) If this is true, I am wasting lots of memory by copying this data for each thread. Is there any way to share the data between threads?

I assume that the 'massiveData' is always copied for each thread (right?) If this is true ...

Nope, false. Only the reference to massiveData is copied.

Java doesn't do magic copies of non-primitive types. If you want to copy something you have to do it explicitly.

If you didn't already know that, I'm guessing you're going to run into all sorts of other problems when you write this multi-threaded code. For example, unless these threads are only reading massiveData , then you really need some sort of synchronization or atomicity guarantees on any updates you make, otherwise you're going to end up with garbage.

Here's a good book on the topic (with Java examples): The Art of Multiprocessor Programming

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM