How to suspend, resume and stop a "Process" and "Dependent thread" in SwingWorker

I am working on applets where I create a GUI like this:

enter image description here

I used SwingWorkerThread

to effectively print 1000 lines for a few seconds on JTable

. Actually from doInBackground()

what I am running a command to write the logs, this is achieved using an instance Process

. Next, there is a separate Thread

one that handles the input captured by the instance Process

.

But the problem is that I cannot implement the functionality of the Pause and Stop buttons. I have seen several posts that implement functions like this:

protected Void doInBackground() throws Exception {
while (!isCancelled()) {
    if (!isPaused()) {
        publish("Writing...");
    } else {
        Thread.sleep(200);
    }
}
return null;

      

}

Problem 1: But the complexity in my code process.waitFor()

is only executed once, I cannot check the "suspended" variable or isCancelled()

method in the loop. Also, for faster implementation, I called the method publish()

from different Thread

( InputConsumer

). I am wondering how it is possible to suspend execution of both threads ( Process

and InputStream

) at the same time.

Problem 2: Implementation of the Stop button. The call worker.cancel(true)

does not stop the execution of threads created by the method doInBackground()

.

The working code, implementing only the Run button, looks like this:

public class LogViewer extends javax.swing.JApplet {
    BufferedReader br;
    DefaultTableModel model;
    TableSwingWorker worker;
    @Override
    public void init() {
        try {
            //initialize GUI
            viewLogs();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void viewLogs() throws IOException{

        model = (DefaultTableModel)jTable1.getModel();
        worker = new TableSwingWorker(model);

    }

    public class TableSwingWorker extends SwingWorker<Integer, Object[]>{
        private DefaultTableModel tableModel;
        public TableSwingWorker(DefaultTableModel tableModel){
            this.tableModel = tableModel;
        }

        @Override
        protected Integer doInBackground() throws Exception {

            String[] command = {"CMD", "/C", "adb logcat"};
            ProcessBuilder probuilder = new ProcessBuilder(command);
            Process process = probuilder.start();

            InputConsumer consumer = new InputConsumer(process.getInputStream());
            consumer.start();

            int result = process.waitFor();
            consumer.join();

            return result;

        }

        @Override
        protected void process(List<Object[]> chunks) {
            for(Object[] row: chunks)
                tableModel.addRow(row);
        }

        protected void processOutput(String text){
            //process text received
            publish(new Object[]{line, date, time, PID, TID, loglevel, tag, message});
        }

        public class InputConsumer extends Thread {
            private InputStream is;

            public InputConsumer(InputStream is){
                this.is = is;
                start();
            }

            @Override
            public void run() {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                try{
                    String text = null;
                    while((text = br.readLine()) != null){
                        processOutput(text);
                    }
                }
                catch(IOException ex){
                    ex.printStackTrace();
                }
            }
        }
    }              
}

      

Any help on this would be deeply appreciated. Please comment for clarification on my part.

+3


source to share





All Articles