After looking through tutorial on http://oreilly.com/catalog/expjava/excerpt/index.html below is what I have written. I wanted thread_B to be able to call one of the methods declared in thread_A
thread_A = new Thread(new Worker());
thread_A.start();
thread_B = new Thread(new Communicator(thread_A));
thread_B.start();
Initializing the thread and assigning it to the class variable seems to work fine. However, it does not seem to allow me to use the thread_A’s method.
//in thread_B,
Thread worker;
public Communicator(Thread worker){
this.worker = worker;
}
//if I want to call thead_A's method size()
public void run(){
this.worker.queue.size(); //DOES NOT WORK
}
I just want thread_B to know about thread_A’s queue information. What is a good approach to solve this problem?
Do it like this:
and change
Communicator‘s constructor to take aWorkerrather than aThread.