【问题标题】:Independent threads in JavaFXJavaFX 中的独立线程
【发布时间】:2014-04-02 05:20:47
【问题描述】:

请考虑以下代码:来源: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 中获取数据,所以我需要从以下位置开始编写代码@Override protected Integer call() throws Exception { 为 10 个线程中的每一个,这将是 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 

【问题讨论】:

  • 如何将作业分配给线程的逻辑完全取决于您。您可以编写 10 个不同的任务,或者您可以有一个类扩展任务并有一个逻辑来分离不同线程的功能!
  • 我明白了。所以我可以有 10 个不同的@Override protected Integer call() throws Exception {,这很好,对吧?我的意思是我有一个扩展 Task&lt;Integer&gt; 的课程,我打算写 10 种不同的`@Override protected Integer call() throws Exception {
  • 你不能有多个定义相同的方法,Java 不允许。你可以试试 !我建议您为 10 个不同的线程设置 10 个不同的任务!
  • @ItachiUchiha 我已经用我想要做的事情更新了我上面的代码。你是对的,Java 不允许我拥有具有相同定义的方法。你能否建议一个为 10 个不同线程拥有/定义多个任务的示例?谢谢
  • 这段代码会给你重复的方法,编译时错误!

标签: java multithreading javafx


【解决方案1】:

我希望你有 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
     }
}

等等..

那么你就可以拥有你的

  1. Thread1 分配给 MyClass1
  2. Thread2 分配给 MyClass2 等等..

将任务分配给线程

MyClass1 objClass1 = new MyClass1();
Thread thread1 = new Thread(objClass1);
thread1.start();

【讨论】:

  • MyClass1 是 Java 文件名为 MyClass1.java 的类 那么,我是否应该在该类中定义其他类,如 classAclassB 等?对于 ProgressBars:我已经定义它们在我的 java 包中的一个单独文件(Class2.java)中,如下所示:ideone.com/nnQ16y 我在上面的代码中调用Class2(更新了我上面的代码)。
  • Contd... 所以基本上,这里定义进度条的方式是基于 10 的值,这导致显示 10 个进度条。你认为我需要在同一个类中为 10 个不同的进度条定义我在类Class2.java 中定义的内容 10 次吗?如果您需要我的更多解释,请告诉我。谢谢@ItachiUchiha
  • 您的代码中有很多编译时错误。您不会使用 QueuyeReaderExecutorService = Executors.newFixedThreadPool(10); 创建 Executor。基本上,我可以建议你从一些非常基本的东西开始。从一个 Thread 和一个 Executor 线程池开始。使用它,然后使用 Tasks 开始增加你的线程!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-12
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 2012-11-01
相关资源
最近更新 更多