我正在设计Javafx中的多线程应用程序,并希望为每个线程的名称和进度添加一个带有列的TableView。做大量的研究后,我发现了什么,我想在这里完成一个类似的例子:从线程多次更新JavaFX ProgressIndicator
JavaFX Update progressbar in tableview from Task
(指向这一点:“https://community.oracle.com/message/10999916”)
我遇到,但问题,在这个例子中很好地说明了;你怎么能多次调用一个'Task'对象来更新一个ProgressIndicator?
我从Oracle的文档中了解到,任务对象“是一次性类,不能重用”。这似乎只能调用Task对象的call()方法一次。我需要多次更新任务,因为它通过一个Thread类进行多次,而不是一次调用它并通过For循环任意递增。
我已阅读关于绑定到监听器和创建服务类的信息,但我不确定这些是否是针对此问题的实际解决方案。因此,我想问一下在Javafx中这是否可行,或者我可能忽略了一些东西。如果有人已经完成了这个过程,如果你能够说明如何通过前面提供的例子,这将是非常有帮助的。
任何方向对此将不胜感激,谢谢。
-DREW
编辑1:我修改了措辞,因为它是不准确的。
编辑2:这里是一些伪代码的例子。说我有下面的代码类:
public static class TaskEx extends Task<Void>{
@Override
protected Void call(){
updateProgress(.5, 1);
return null
}
public static void callThread() {
TableView<TaskEx> table = new TableView<TaskEx>();
//Some code for data in table.
TableColumn progressColumn = new TableColumn ("Progress");
progressColumn.setCellValueFactory(new PropertyValueFactor("progress");
table.setItems(<data>);
table.getColumns();addAll(progressColumn);
ExecutorService executor = Executors.newFixedThreadPool(<SomeNumber>);
for(TaskEx task : table.getItems(){
Threading.ThreadClass newThread = new Threading.ThreadClass(task);
executor.submit(newThread, <uniqueID>);
}
}
然后说我有一个第二类与此逻辑线程:
static class ThreadClass extends Thread{
Task progressTask;
public ThreadClass(Task task, Integer id){
progressTask = task;
}
public void run(){
ExecutorService executor = Executors.newFixedThreadPool(<someNumber>);
//This invokes the Task call for the correct progressIndicator in the Tableview.
//It will correctly set the progressIndicator to 50% done.
executor.submit(progressTask);
/* Main logic of the Threading class that involves the 'id' passed in. */
//This will do nothing because you cannot invoke the Task call more than once.
executor.submit(progressTask);
}
}
这是哪门子的工作流程,我需要的,但我不确定如何做到这一点。
*“这似乎那时,人们只能调用任务对象‘的UpdateProgress’一次。” *这显然是不正确的:比如你挂电话'的UpdateProgress(...)'在一个循环,所以每个任务都会多次调用它。所有的文档都意味着你只能调用Task的'call()'方法一次。您可以在'call()'方法内多次调用'updateProgress'。你链接的例子似乎是你描述你想要完成的完整实现。你能解释一下你的问题有什么不同吗? –
嘿詹姆斯。我确信我说得那么糟糕。我的意思是,为了正确地增加任务的进度,我需要多次调用Task的call()方法。我知道我可以循环并在调用update()方法后继续调用updateProgress,但我不知道当我调用它时线程将花费多长时间完成。我曾计划在线程逻辑中的某个特定位置调用Task的call()方法一次,然后再次调用该方法,等等。 –
我想你一定误解了“任务”的目的?该任务代表“你正在做的事情” - 所以它应该是你感兴趣的任务的进展。如果情况并非如此,那么您使用“Task”作为什么?你可以张贴一些代码来说明问题吗? –