-1

By following the answer here: How to know if other threads have finished?

I manage to build a listener for a thread like so:

    NotifyingThread prcoessCatRegThread =  new processCatFromPage(baseURL, 0, null);
        prcoessCatRegThread.addListener(this);
        prcoessCatRegThread.start();

    @Override
    public void notifyOfThreadComplete(NotifyingThread notifyingThread) {
        // TODO Auto-generated method stub
        System.out.println("Thread calledback returned.");
        System.out.println(notifyingThread);
    }

And the "processCatFromPage" Class:

class processCatFromPage extends NotifyingThread{
    int fromPage = 0;
    Integer toPage = 0;
    int cat = 0;
    String baseURL = null;
    static List<Posts> Posts = new ArrayList<Posts>();

    private void processPage(int pageNum){

    }

    processCatFromPage(String baseURL, int fromPage, Integer toPage){

    }


    @Override
    public void doRun() {
        processPage(fromPage);
    }
}

Now i try to access "Posts" from the "processCatFromPage" class in the "notifyOfThreadComplete", when debugging in eclipse i can see the variable, but i dont know how can i access to it..

notifyingThread.Posts

Comesup with an error.. and so any other method i tryed..

Any help?

Many Thanks!:)

The variable i try to acess trough the code: https://i.stack.imgur.com/dWwgY.png

Community
  • 1
  • 1
Itay Elkouby
  • 344
  • 1
  • 15
  • Why don't you use an `ExecutorService`? – fge Apr 13 '15 at 11:55
  • Does this answer your Question? http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fviews%2Fvariables%2Fref-variables_view.htm. If not, then please explain where you are typing "notifyingThread.Posts" ... to get it to give you errors. – Stephen C Apr 13 '15 at 12:08
  • @StephenC i see the variable in the Variables View windows like in the link you attached, but cant reach it in the code, The "notifyingThread.Posts" is example when i try to type: public void notifyOfThreadComplete(NotifyingThread notifyingThread) { // TODO Auto-generated method stub System.out.println("Thread calledback returned."); System.out.println(notifyingThread.Posts); } – Itay Elkouby Apr 13 '15 at 12:13
  • @fge the listener is more suitable for the purposes i need, Thanks anyway! – Itay Elkouby Apr 13 '15 at 12:24
  • What precisely is the error message? – Stephen C Apr 13 '15 at 12:44
  • @StephenC Posts cannot be resolved or is not a field – Itay Elkouby Apr 13 '15 at 12:47
  • See Pete Kirkham's answer. This explains how to access a `static` variable, and that seems to be your problem. – Stephen C Apr 13 '15 at 13:59

1 Answers1

1

The debugger looks at the runtime type of the object when looking for members, which is processCatFromPage, the compiler looks at the compile time type of notifyingThread which you declared to be NotifyingThread.

The simple fix is that Posts is a static member of the processCatFromPage class, so could be accessed using processCatFromPage.Posts, and making it public if accessed outside of scope. You could also cast notifyingThread so the compiler knows it's a processCatFromPage and access the member that way, but this is a bug in the Java language.

Neither globally public static members nor downcasting have any place in well structured object-oriented code, and it is especially bad to use static state in concurrent applications

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • Thank you very much!! Little question: When not declare Posts as static, i can access it from the casting "notifyingThread" Object, i would like to know more about that "bug" and why does it happen, does it make it not recommended to use? – Itay Elkouby Apr 14 '15 at 07:39
  • The 'bug' is that the you don't need an instance to access the the field, and that there is syntax which doesn't use an instance to access the field, so there's two syntaxes which do the same thing, one of which looks like an access of a member of an instance but isn't - http://stackoverflow.com/questions/3293353/how-come-invoking-a-static-method-on-a-null-reference-doesnt-throw-nullpointe . Since this wasn't noticed until after code was written to use it, they can't remove it from the language - http://stackoverflow.com/questions/610458 . – Pete Kirkham Apr 14 '15 at 09:09