1

I've some issues with a Javafx GUI that I'm updating . In this GUI I have a table view , a textarea and a panel (with progress indicators) where I show the progress of the tasks i'm doing in background . Each task is a command executed by a ProcessBuilder.

Everything works well , however after several tasks , the tableview , the textarea and the progress indicator that I'm updating start lagging. For my tasks in background I created a custom class which extends the Task Class:

private String contenu="" ;
private TextArea cmdPrompt;
private ProcessBuilder pro;
private String [] myCmd;
private int count=0;
private String scriptName;


public CustomTask(TextArea cmdProm,ProcessBuilder probuilder,String[] cmd, String name)
{
    this.cmdPrompt=cmdProm;
    this.pro=probuilder;
    this.myCmd=cmd;
    this.scriptName=name;
    messageProperty().addListener(new ChangeListener<String>(){

        @Override
        public void changed(ObservableValue<? extends String> arg0,
                String arg1, String arg2) {
            cmdPrompt.appendText(contenu);

        }});





}
@Override
protected Object call() throws Exception {
try {   
    Process p=pro.start();      
    BufferedReader input= new   BufferedReader(newInputStreamReader(p.getInputStream())); 
    String line; 
    String tmpon=""; 
    tmpon=("\n "); 
    while ((line = input.readLine()) != null) 
      { 
       tmpon = tmpon+"\n"+line;
       System.out.println(line); 
      } 
    contenu=tmpon; 
    input.close(); 
    p.waitFor(); 
  } 
 catch (Exception e) {
   System.out.println("Task Exception");
 }

     updateMessage(contenu);
     updateProgress(1, 1);
     return null;
}

My function which calls the tasks is working like this :

final CustomTask task1= new CustomTask(cmdPrompt,probuilder,cmd,Namescript);
final CustomTask task2= new CustomTask(cmdPrompt,probuilder,cmd2,Namescript);
final CustomTask task3= new CustomTask(cmdPrompt,probuilder,cmd3,Namescript);
final CustomTask task4= new CustomTask(cmdPrompt,probuilder,cmd4,Namescript);
final CustomTask task5= new CustomTask(cmdPrompt,probuilder,cmd5,Namescript);
final CustomTask task6= new CustomTask(cmdPrompt,probuilder,cmd6,Namescript);

pi1.progressProperty().bind(task3.progressProperty());
pi2.progressProperty().bind(task4.progressProperty()); 
pi3.progressProperty().bind(task5.progressProperty());
pi4.progressProperty().bind(task6.progressProperty());
new Thread(task1).start();
task1.setOnSucceeded(new EventHandler<WorkerStateEvent>() {

            @Override
            public void handle(WorkerStateEvent t) {

                    new Thread(task2).start();
            }
        });


task2.setOnSucceeded(new EventHandler<WorkerStateEvent>() {

                @Override
                public void handle(WorkerStateEvent t) {

                        new Thread(task3).start();
                }
            });

As you can easily imagine , I wait every time for the success of the previous task to do the next one . This function is recursive , in fact after the success of the last task (task6) I'm recalling the function to do the same stuff for other data .

After some tests I found that the gui elements are lagging after 5-6 calls of the function.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rekta
  • 11
  • 2
  • What are you doing with the [ProcessBuilder](http://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html)? It's not synchronized and only for OS processes. – aw-think Jun 30 '15 at 13:57
  • essentially executing .bat and .exe files . For each task i just launch the program and i redirect the output to my textarea . – Rekta Jun 30 '15 at 14:00
  • Is the class CustomTask derived from [JavaFX Task](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html)? – aw-think Jun 30 '15 at 14:01
  • yes of course it is. There is exacly what I'm doing in my call function: try { Process p=pro.start(); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; String tmpon=""; tmpon=("\n "); while ((line = input.readLine()) != null) { tmpon = tmpon+"\n"+line; System.out.println(line); } contenu=tmpon; input.close(); p.waitFor(); } catch (Exception e) { }// then i update – Rekta Jun 30 '15 at 14:05
  • Ok, well I think your ProcessBuilder is the problem. Because all threads are finished when the new thread is created, this is the only possible thing where it breaks. ProcessBuilder creates heavy weight processes. Look here: http://stackoverflow.com/questions/3943431/execute-an-external-command-in-java – aw-think Jun 30 '15 at 14:15
  • I tried to destroy the process after execution but i still have the same problems of slowing/laging – Rekta Jun 30 '15 at 14:46
  • Can you edit your question to include the code you put in the comment? It is really hard to read in a comment... Are you sure your are not getting exceptions thrown? You should at least display the stack trace in the `catch` block. – James_D Jun 30 '15 at 18:35

0 Answers0