简体   繁体   中英

How can i use callback method on my java code?

public class DowloadEngine implements Runnable {

public DowloadEngine(CallBack c) {
    callback = c;
}

public interface CallBack {
    public void processDone(String message);
}

private final CallBack callback;

@Override
public void run() {
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {}
    callback.processDone("'CallBack' func is called");
}
}

And there is my main class in here

public class GUI implements DowloadEngine.CallBack{

public static void main(String[] args){

    Thread thread = new Thread(new DowloadEngine(this));// Error :Make main not static!!
    thread.start();

    //wait a little to see the result
    Scanner scan = new Scanner(System.in);
    scan.nextLine();
    //wait a little to see the result
}

@Override
public void processDone(String message) {
    //code ...
    //code ...
    //code ...
    System.out.println(message);
}
}

I want to do all works on main class via callback method but I did not understand these methodology. How does it works? How can i use these with together?

Change:

Thread thread = new Thread(new DowloadEngine(this)); to

Thread thread = new Thread(new DowloadEngine(new GUI()));

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