【问题标题】:Continuous running Tasks in javafx?javafx中连续运行的任务?
【发布时间】:2017-01-10 06:52:05
【问题描述】:
private Task<Void> getTransferId = new Task<Void>() {

    @Override
    protected Void call() throws Exception {
        Client client = ClientBuilder.newClient();
        WebTarget webTarget = client.target(Constants.BASE_URL);
        WebTarget helloworldWebTarget = webTarget.path(Constants.TRANSFER_FILE);
        System.out.println(" checking working.....status");
        Invocation.Builder invocationBuilder = helloworldWebTarget.request(MediaType.APPLICATION_JSON);
        PostFileLog fileLog = new PostFileLog();
        fileLog.setSenderId(myUuid);
        fileLog.setReceiverId(clickedPossitionUuid);
        fileLog.setFileName(fileName);
        fileLog.setFileType(fileTypeInt);
        fileLog.setFileSize(fileSizeFinal);
        fileLog.setStatus(Constants.STATUS_PENDING);
        fileLog.setFileDesc(Constants.NO_CAPTION);
        Response response = invocationBuilder.post(Entity.entity(fileLog, MediaType.APPLICATION_JSON));
        int status = response.getStatus();
        PostFileLog reponceLog = response.readEntity(PostFileLog.class);
        transferId = reponceLog.getUuid();
        System.out.println(transferId + " fist sending TrasferId andstastus " + status);

        return null;
    }
};

为了执行上面的代码,我使用.run(); 它只执行一次。我需要连续执行这个问题,任何人都可以帮助我解决这个问题吗?

【问题讨论】:

  • "为了执行上述代码,我使用的是.run();"。这不会在后台线程中运行任务:它将在当前线程上运行它(可能是 FX 应用程序线程,取决于您从哪里调用它)。您应该将其包装在Thread 中并在线程上调用start(),或者将其提交给适当的Executor

标签: java javafx javafx-2


【解决方案1】:

只需将代码放入 while 循环中,然后停止 while 循环

编辑你的代码检查这个

boolean isStop=false;
private Task getTransferId = new Task()
{

@Override
protected Void call() throws Exception

{
    while(!isStop){
    Client client = ClientBuilder.newClient();
    WebTarget webTarget = client.target(Constants.BASE_URL);
    WebTarget helloworldWebTarget = webTarget.path(Constants.TRANSFER_FILE);
    System.out.println(" checking working.....status");
    Invocation.Builder invocationBuilder = helloworldWebTarget.request(MediaType.APPLICATION_JSON);
    PostFileLog fileLog = new PostFileLog();
    fileLog.setSenderId(myUuid);
    fileLog.setReceiverId(clickedPossitionUuid);
    fileLog.setFileName(fileName);
    fileLog.setFileType(fileTypeInt);
    fileLog.setFileSize(fileSizeFinal);
    fileLog.setStatus(Constants.STATUS_PENDING);
    fileLog.setFileDesc(Constants.NO_CAPTION);
    Response response = invocationBuilder.post(Entity.entity(fileLog, MediaType.APPLICATION_JSON));
    int status = response.getStatus();
    PostFileLog reponceLog = response.readEntity(PostFileLog.class);
    transferId = reponceLog.getUuid();
    System.out.println(transferId + " fist sending TrasferId andstastus " + status);
 }
    return null;
}
};

public void StopTask(){//To stop the task
isStop=true;
}

【讨论】:

  • 如果你使用一个标志,至少让它变得易变。否则Task 线程可能永远不会看到更新的值。但是我建议改用cancel+isCancelled...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-08
  • 2018-11-14
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
  • 2018-10-18
  • 1970-01-01
相关资源
最近更新 更多