Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在多线程处理中为变量赋值_Java_Multithreading - Fatal编程技术网

Java 在多线程处理中为变量赋值

Java 在多线程处理中为变量赋值,java,multithreading,Java,Multithreading,我已经编写了一个代码来在两个客户端和服务器之间发送和接收文件,但我希望在一个程序中同时使用这两个函数。因此,我编写了一个单独的类,其中包含发送和接收函数。我声明了两个变量sendFilePath和receiveFilePath。我调用了这个方法 传输(String sendFileName,String receiveFileName)并为这些变量赋值,但显示为null 我该怎么做 实际上我想发送sendFilePath到rf.receive()和receiveFilePath到sf.send(

我已经编写了一个代码来在两个客户端和服务器之间发送和接收文件,但我希望在一个程序中同时使用这两个函数。因此,我编写了一个单独的类,其中包含发送和接收函数。我声明了两个变量
sendFilePath
receiveFilePath
。我调用了这个方法
传输(String sendFileName,String receiveFileName)
并为这些变量赋值,但显示为null

我该怎么做

实际上我想发送
sendFilePath
rf.receive()
receiveFilePath
sf.send()方法如何实现

class TransferFile implements Runnable
{    
    Thread thread;

    String sendFilePath, receiveFilePath;
    SendFile sf = new SendFile();
    ReceiveFile rf = new ReceiveFile();

    public void TransferFileCreateThread(String name)
    {
        thread = new Thread(this,name);
        thread.start();
    }
    public void run()
    {
        rf.receive(sendFilePath);
        sf.send(receiveFilePath);
    }

    public void transfer(String sendFileName, String receiveFileName)
    {
        try
        {
            sendFilePath = sendFileName;
            receiveFilePath = receiveFileName;

            TransferFile t1 = new TransferFile();
            TransferFile t2 = new TransferFile();
            t1.TransferFileCreateThread(sendFilePath);
            t2.TransferFileCreateThread(receiveFilePath);
        }
        catch(Exception e)
        {
        }
    }
}

这里您正在创建新对象t1和t2, 您没有将值指定给对象t1和t2

你可以这样做

class TransferFile implements Runnable
{    
    Thread thread;

    private String sendFilePath=null;
    private String receiveFilePath=null;
    SendFile sf = new SendFile();
    ReceiveFile rf = new ReceiveFile();

    public void TransferFileCreateThread(String name)
    {
        thread = new Thread(this,name);
        thread.start();
    }
    public void run()
    {
        rf.receive(sendFilePath);
        sf.send(receiveFilePath);
    }
    public void setSendFilePath(String filePath){
         this.sendFilePath=filePath;
    }
    public void setReceiveFilePath(String filePath){
         this.receiveFilePath=filePath;
    }
    public void transfer(String sendFileName, String receiveFileName)
    {
        try
        {
            sendFilePath = sendFileName;
            receiveFilePath = receiveFileName;

            TransferFile t1 = new TransferFile();
            t1.setSendFilePath(sendFilePath);
            t1.setReceiverFilePath(receiveFilePath);
            TransferFile t2 = new TransferFile();
            t1.setSendFilePath(sendFilePath);
            t1.setReceiverFilePath(receiveFilePath);
            t1.TransferFileCreateThread(sendFilePath);
            t2.TransferFileCreateThread(receiveFilePath);
        }
        catch(Exception e)
        {
        }
    }
}
希望能有帮助


您可以做的另一件事是,您可以在t1和t2上调用Transfer方法,以便它将值指定给字符串对象。

不清楚您在问什么,请用类的用法片段和注释澄清它,说明什么是空的,问题到底出在哪里。。。这些问题要么不完整,要么你认为阅读它的人会知道你的情况。请详细说明。我指定了sendFilePath=sendFileName;receiveFilePath=receiveFileName;传输方法中的值,但在运行方法中,sendFilePath和receiveFilePath的值为null。为什么它显示为null?我有点困惑:您试图发送/接收文件,但传输的是路径?代码看起来不完整,或者与问题不匹配。您说希望“将sendFilePath发送到rf.receive();”,但当您将其作为参数传递时,您就是这样做的,对吗?