请考虑以下代码:来源:http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm
import javafx.concurrent.Task;
Task<Integer> task = new Task<Integer>() {
@Override protected Integer call() throws Exception {
int iterations;
for (iterations = 0; iterations < 1000; iterations++) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
updateMessage("Iteration " + iterations);
updateProgress(iterations, 1000);
//Block the thread for a short time, but be sure
//to check the InterruptedException for cancellation
try {
Thread.sleep(100);
} catch (InterruptedException interrupted) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
}
}
return iterations;
}
};
我正在学习JavaFX并想知道,如果我必须创建10个独立的线程。比方说,Thread 1
将从位于IP 11.11.1.111
的MySQL获取数据,Thread 2
将从位于IP 22.22.2.222
的MySQL获取数据,所以我需要为10个线程中的每个线程编写从@Override protected Integer call() throws Exception {
开始的代码,这将是10次?
这就是我现在所拥有的:
public class MyClass extends Task<Integer>
{
private Class2 QRVC ;
// my variable declarations here
@Override protected Integer call() throws Exception {
// Defining logic to grab data from 11.11.1.111
// Updating the progress bar
}
@Override protected Integer call() throws Exception {
// Defining logic to grab data from 22.22.2.222
// Updating the progress bar
}
@Override protected Integer call() throws Exception {
// Defining logic to grab data from 33.33.3.333
// Updating the progress bar
}
}// END OF Class MyClass
答案 0 :(得分:0)
我希望你有10个不同的进度条。
只需制作10个不同的类,这些类扩展Task
并将您的逻辑放入单独的call()
public class MyClass1 extends Task<Integer>
{
// my variable declarations here
@Override protected Integer call() throws Exception {
// Defining logic to grab data from 11.11.1.111
// Updating the progress bar
}
}
public class MyClass2 extends Task<Integer>
{
// my variable declarations here
@Override protected Integer call() throws Exception {
// Defining logic to grab data from 22.22.2.222
// Updating the progress bar
}
}
依旧......
然后你可以拥有你的
将任务分配给线程
MyClass1 objClass1 = new MyClass1();
Thread thread1 = new Thread(objClass1);
thread1.start();