Still in the phase of learning oop concepts in Java. In the concurrency example available in oracle tutorial here ((please bear with me and have a quick look at it, it is easy)), producerconsumerexample
class triggers the two threads of producer
and consumer
which both should exchange data through the Drop
object drop
. And within this object they are supposed to wait for/get notified by each other.
public class ProducerConsumerExample {
public static void main(String[] args) {
Drop drop = new Drop();
(new Thread(new Producer(drop))).start();
(new Thread(new Consumer(drop))).start();
}
}
What I do not understand is that the drop
object is passed to each thread by the producerconsumerexample
class, which gives us two new local Drop
objects, also named drop
, one in each of the two threads. To my understanding, this is because passing in Java is by value, not reference. Thus, each thread now has its own version of the Drop
object, so how come they are still supposed to share data through the same original Drop
object, since each thread has its own version ?!
Somebody please help, l would really really really appreciate it.