Java不止一次运行Task <void>

时间:2018-04-23 23:15:32

标签: java multithreading javafx task

我有以下问题:我有一个任务:

package window;

import javafx.concurrent.Task;
import javafx.scene.control.TextField;

import javax.xml.soap.SOAPMessage;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientConnection extends Task<Void> {
    public Socket clientSocket;
    private DataInputStream is = null;
    private DataOutputStream os = null;
    private String messageID;
    private String nodeNameToSend;
    private int layerNumberToSend;
    private String messageTypeToSend;
    private String messageToSend;

    private int portNumber;
    private String name;

    private String soapToString(SOAPMessage message) throws TransformerException {
        StringWriter sw = new StringWriter();
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource((message.getSOAPPart())), new StreamResult(sw));
        return sw.toString();
    }


    public ClientConnection(String name, int portNumber) throws IOException {
        this.name = name;
        this.portNumber = portNumber;
        clientSocket = new Socket(name, portNumber);
    }

    @Override
    protected Void call() throws Exception {
        System.out.println("trying to send in separate thread");
        try {
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream(), "UTF-8"));
            SoapWrapper soapWrapper = new SoapWrapper();
            // Creating the message
            MessageWrapper messageWrapper = new MessageWrapper(messageID, nodeNameToSend, layerNumberToSend, messageTypeToSend, messageToSend);
            SOAPMessage msg = soapWrapper.toMessage(messageWrapper);
            out.write(soapToString(msg));
            out.flush();
            out.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setArgs(String messageID, String name, int layerNumber, String messageType, String messageContent) {
        this.messageID = messageID;
        this.nodeNameToSend = name;
        this.layerNumberToSend = layerNumber;
        this.messageTypeToSend = messageType;
        this.messageToSend = messageContent;
    }
}

在我的JavaFX控制器中,我使用全局ClientConnection cc = new ClientConnection("localhost", 2143);运行它,然后在new Thread(cc).start();之间运行它。一切正常(它适用于套接字),trying to send in separate thread将打印到控制台。但是,如果我想再次这样做(在我的情况下再次按下按钮)它没有做任何事情,trying to send in separate thread没有打印。我能做什么?我已经读过你必须创建一个Thread的新实例,但我这样做了,对吧?

1 个答案:

答案 0 :(得分:0)

如果您说有全局 ClientConnection,我认为您的意思是您尝试为每次运行重复使用ClientConnection的相同实例。这不起作用,因为Task不可重复使用as stated in the documentation

  

与FutureTask一样,Task是一次性类,不能重复使用。

因此,您需要确保每次运行时使用新的cc覆盖ClientConnection变量(即分配cc = new ClientConnection(...) - 在您的情况下,这将是第一件事当你进入按钮的动作处理程序时,你会这样做。)