如何从自定义类内部更新进度

huangapple go评论54阅读模式
英文:

How to update progress from within a custom class

问题

我需要在工作/任务线程内部的自定义类中更新进度。我该如何做?

public Task createWorker() {
    Task task = new Task() {

        @Override
        protected Object call() throws Exception {
            DocumentProcessor dp = new DocumentProcessor();
            dp.process(this);
        }

        public void notifyTask(String _msg, long _stepNumber, long _totalSteps ){
            updateMessage(_msg);
            updateProgress(_stepNumber, _totalSteps);
        }
    };
}

类:

public class DocumentProcessor {
    public void process(Task _task){
         // 步骤 1
         _task.notifyTask("步骤 1 完成", 1, 2);

         // 步骤 2
         _task.notifyTask("步骤 2 完成", 2, 2);

    }
}

调用:

Task worker = createWorker();
new Thread(worker).start();

notifyTask 函数现在可用。

英文:

I need to update the progress from a custom class inside a worker/task thread. How do I do this?

public Task createWorker() {
    Task task = new Task() {

        @Override
        protected Object call() throws Exception {
            DocumentProcessor dp = new DocumentProcessor();
            dp.proces(this);
        }

        public void notifyTask(String _msg, long _stepNumber, long _totalSteps ){
            updateMessage(_msg);
            updateProgress(_stepNumber, _totalSteps);
        }
    };
}

class:

public class DocumentProcessor {
    public void proces(Task _task){
         // Step 1
         _task.notifyTask("Step 1 done", 1 ,2); // ERROR: no function with that name

         // Step 2
         _task.notifyTask("Step 2 done", 2 ,2);

    }
}

invoke

    Task worker = createWorker();
    new Thread(worker).start();

The notifyTask function is not available.

答案1

得分: 1

The notifyTask method is not available because it's not included in the Task class, but defined in your (anonymous) subclass.

You can for example create a named subclass and pass that to the DocumentProcessor.

public class NotifiableTask extends Task {
    @Override
    protected Object call() throws Exception {
        DocumentProcessor dp = new DocumentProcessor();
        dp.process(this);
    }

    public void notifyTask(String _msg, long _stepNumber, long _totalSteps ){
       updateMessage(_msg);
       updateProgress(_stepNumber, _totalSteps);
   }
}
public Task createWorker() {
   return new NotifiableTask();
}

public class DocumentProcessor {
    public void process(NotifiableTask _task){
       // Step 1
       _task.notifyTask("Step 1 done", 1 ,2); // function is now available because it's defined in NotifiableTask
       // Step 2
       _task.notifyTask("Step 2 done", 2 ,2);

    }
}

Task worker = createWorker();
new Thread(worker).start();

You can also define an interface and have your class implement that for looser coupling.

public interface Notifiable {
    void notifyTask(String _msg, long _stepNumber, long _totalSteps);
}

public class NotifiableTask extends Task implements Notifiable {
    // implement Task's call()
    @Override
    protected Object call() throws Exception {
        DocumentProcessor dp = new DocumentProcessor();
        dp.process(this);
    }

    // implement Notifiable's notifyTask
    public void notifyTask(String _msg, long _stepNumber, long _totalSteps ){
       updateMessage(_msg);
       updateProgress(_stepNumber, _totalSteps);
   }
}
public Task createWorker() {
   return new NotifiableTask();
}

public class DocumentProcessor {
    // we don't need Task's call() method here, so pass the Notifiable only
    public void process(Notifiable _task){
       // Step 1
       _task.notifyTask("Step 1 done", 1 ,2); // function is now available because it's declared in Notifiable
       // Step 2
       _task.notifyTask("Step 2 done", 2 ,2);

    }
}

Task worker = createWorker();
new Thread(worker).start();

You'll probably need to declare NotifiableTask as an inner class of wherever you currently define the createTask method because it needs to call its other methods (updateMessage() and updateProgress()) during notifyTask(). In fact, since the task itself only delegates this call, you might as well declare the interface in the outer class and perform the update there; but this depends on the context and is hard to tell since you obviously only provided a minimized example.

英文:

The notifyTask method is not available because it's not included in the Task class, but defined in your (anonymous) subclass.

You can for example create a named subclass and pass that to the DocumentProcessor.

public class NotifiableTask extends Task {
    @Override
    protected Object call() throws Exception {
        DocumentProcessor dp = new DocumentProcessor();
        dp.proces(this);
    }

    public void notifyTask(String _msg, long _stepNumber, long _totalSteps ){
       updateMessage(_msg);
       updateProgress(_stepNumber, _totalSteps);
   }
}
public Task createWorker() {
   return new NotifiableTask();
}

public class DocumentProcessor {
    public void proces(NotifiableTask _task){
       // Step 1
       _task.notifyTask("Step 1 done", 1 ,2); // function is now available because it's defined in NotifiableTask
       // Step 2
       _task.notifyTask("Step 2 done", 2 ,2);

    }
}

Task worker = createWorker();
new Thread(worker).start();

You can also define an interface and have your class implement that for looser coupling.

public interface Notifiable {
    void notifyTask(String _msg, long _stepNumber, long _totalSteps);
}

public class NotifiableTask extends Task implements Notifiable {
    // implement Task's call()
    @Override
    protected Object call() throws Exception {
        DocumentProcessor dp = new DocumentProcessor();
        dp.proces(this);
    }

    // implement Notifiable's notifyTask
    public void notifyTask(String _msg, long _stepNumber, long _totalSteps ){
       updateMessage(_msg);
       updateProgress(_stepNumber, _totalSteps);
   }
}
public Task createWorker() {
   return new NotifiableTask();
}

public class DocumentProcessor {
    // we don't need Task's call() method here, so pass the Notifiable only
    public void proces(Notifiable _task){
       // Step 1
       _task.notifyTask("Step 1 done", 1 ,2); // function is now available because it's declared in Notifiable
       // Step 2
       _task.notifyTask("Step 2 done", 2 ,2);

    }
}

Task worker = createWorker();
new Thread(worker).start();

You'll probably need to declare NotifiableTask as an inner class of wherever you currently define the createTask method because it needs to call its other methods (updateMessage() and updateProgress()) during notifyTask(). In fact, since the task itself only delegates this call, you might as well declare the interface in the outer class and perform the update there; but this depends on the context and is hard to tell since you obviously only provided a minimized example.

huangapple
  • 本文由 发表于 2023年6月15日 17:04:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定