Javafx Gui lagging / Task update

时间:2015-06-30 13:41:07

标签: java multithreading javafx task

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.

0 个答案:

没有答案