-1

I'm trying to reference thread a from thread b, I essentially want to use the getN() method in B class/thread, any help is appreciated

//// class help {
    ///// main {

        Thread a = new Thread(new A());
        Thread b = new Thread(new B(a));
    }
}

class A implements Runnable {
    private static int tally;
    public void run() {

    }
    public int getN() {
        tally = 6;
        return tally;
    }
}

class B implements Runnable {
    private A aref;
    public B(A ref){
        aref=ref;
    }
    public void run() {
        aref.getN();
    }
}

////////////////////////////////////////////////////////////////// /////////////////////////

MF DOOM
  • 413
  • 4
  • 19
  • Why is `A` a `Runnable` that gets put on a thread when it doesn't do anything (it's `run` method contains no operations)? I take it this is "just an example" but, if so, it's a bad one that doesn't make any sense. – Michael Apr 24 '18 at 15:11
  • Do you expect `aref.getN()` to be executed on thread A? – CannedMoose Apr 24 '18 at 15:12
  • @CannedMoose yes, I essentially want my B thread to be able to use the getN() method so I can reach the tally value – MF DOOM Apr 24 '18 at 15:16
  • @Michael it was just an example, but thanks for the very helpful response – MF DOOM Apr 24 '18 at 15:21
  • @ghost if you only read a value of the other thread you should, like others already mentioned use some sort of synchronization, at least `AtomicInteger`or similar. If you are doing longer computations, you need to be aware that the call to `getN()` is executed by/in the context of thread B and not on A like you might have expected. – CannedMoose Apr 24 '18 at 15:48

1 Answers1

2

In order to construct an object of class B you need a reference to object of class A, not to the object of class Thread. So this should work:

A objA = new A();
Thread a = new Thread(objA);
Thread b = new Thread(new B(objA));
  • Cheers, then would I use b.start(); System.out.println(b.getValue()); but it says i cannot find the value? – MF DOOM Apr 24 '18 at 15:20
  • @ghost `b` is an instance of `Thread`. It has no `getValue` method (and neither do classes `A` or `B` for that matter) – Michael Apr 24 '18 at 15:27
  • @thejavaguy-ivan-milosavljevi when im doing b.start() the getN() method doesnt seem to working – MF DOOM Apr 24 '18 at 15:27
  • ah cheers anyway, messed up somewhere, was trying to make it so my second thread read in numbers from my first thread – MF DOOM Apr 24 '18 at 15:29
  • What do you mean by "doesn't seem to be working"? If you change the implementation of the method `run()` in class `B` to `System.out.println(aref.getN());` it should print `6`. – TheJavaGuy-Ivan Milosavljević Apr 24 '18 at 15:29
  • @ghost If `tally` might get changed after the threads `start`, you should use some synchronization mechanism – xingbin Apr 24 '18 at 15:40
  • @user6690200 what about if i set tally to random numbers, the numbers change once it reachs my B thread – MF DOOM Apr 24 '18 at 16:55