Getting the output of a Thread

后端 未结 7 1527
时光说笑
时光说笑 2021-02-02 01:28

What do you think is the best way for obtaining the results of the work of a thread? Imagine a Thread which does some calculations, how do you warn the main program the calculat

7条回答
  •  逝去的感伤
    2021-02-02 01:31

    You could create a lister interface that the main program implements wich is called by the worker once it has finished executing it's work.

    That way you do not need to poll at all.

    Here is an example interface:

    /**
     * Listener interface to implement to be called when work has
     * finished.
     */
    public interface WorkerListener {
        public void workDone(WorkerThread thread);
    }
    

    Here is an example of the actual thread which does some work and notifies it's listeners:

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    /**
     * Thread to perform work
     */
    public class WorkerThread implements Runnable {
        private List listeners = new ArrayList();
        private List results;
    
        public void run() {
            // Do some long running work here
    
            try {
                // Sleep to simulate long running task
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            results = new ArrayList();
            results.add("Result 1");
    
            // Work done, notify listeners
            notifyListeners();
        }
    
        private void notifyListeners() {
            for (Iterator iter = listeners.iterator(); iter.hasNext();) {
                WorkerListener listener = (WorkerListener) iter.next();
                listener.workDone(this);
            }
        }
    
        public void registerWorkerListener(WorkerListener listener) {
            listeners.add(listener);
        }
    
        public List getResults() {
            return results;
        }
    }
    

    And finally, the main program which starts up a worker thread and registers a listener to be notified once the work is done:

    import java.util.Iterator;
    import java.util.List;
    
    /**
     * Class to simulate a main program
     */
    public class MainProg {
        public MainProg() {
            WorkerThread worker = new WorkerThread();
            // Register anonymous listener class
            worker.registerWorkerListener(new WorkerListener() {
                public void workDone(WorkerThread thread) {
                    System.out.println("Work done");
                    List results = thread.getResults();
                    for (Iterator iter = results.iterator(); iter.hasNext();) {
                        String result = (String) iter.next();
                        System.out.println(result);
                    }
                }
            });
    
            // Start the worker thread
            Thread thread = new Thread(worker);
            thread.start();
    
            System.out.println("Main program started");
        }
    
        public static void main(String[] args) {
            MainProg prog = new MainProg();
        }
    }
    

提交回复
热议问题