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_Synchronization - Fatal编程技术网

Java 在客户机-服务器体系结构中处理多个线程以实现文件同步

Java 在客户机-服务器体系结构中处理多个线程以实现文件同步,java,multithreading,synchronization,Java,Multithreading,Synchronization,我必须用Java实现一个客户机-服务器应用程序,根据客户端文件中的更改自动更新服务器目录中的txt文件,以完成家庭作业(不得不这样做,因为我已经过了最后期限) 我有一个包可以正确地处理文件中的更改,但我对如何处理多个文件中的更改感到困惑。我的方法是为客户机目录中的每个文件使用单独的线程,并出于同样的原因在服务器目录中使用相应的线程。这种方法适用于单个文件,但不适用于多个文件 下面的代码位于客户端,调用文件线程的checkfilestate方法来处理更新 while(true){ for

我必须用Java实现一个客户机-服务器应用程序,根据客户端文件中的更改自动更新服务器目录中的txt文件,以完成家庭作业(不得不这样做,因为我已经过了最后期限)

我有一个包可以正确地处理文件中的更改,但我对如何处理多个文件中的更改感到困惑。我的方法是为客户机目录中的每个文件使用单独的线程,并出于同样的原因在服务器目录中使用相应的线程。这种方法适用于单个文件,但不适用于多个文件

下面的代码位于客户端,调用文件线程的checkfilestate方法来处理更新

while(true){
    for (Map.Entry<String, SynchronisedFile> entry : fileList.entrySet())  {  
    try {
        System.err.println("SyncTest: calling fromFile.CheckFileState()");
        sstt.start();
        entry.getValue().CheckFileState();

    } catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    } catch (InterruptedException e) {
        e.printStackTrace();
        System.exit(-1);
    }
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
        System.exit(-1);
    }
    }
它按预期工作。但如果我同时启动服务器端线程(其中包含解码输入流中Json消息的方法),请使用:


如果需要的话,我很乐意提供更多信息。从设计角度来看,我想知道为什么客户端和服务器“紧密耦合”:为什么客户端需要知道服务器正在使用多少线程;它甚至必须创造它们?!您的服务器应提供更新文件的服务;服务器如何实现此服务(使用一个或多个线程)应该对该服务的用户完全隐藏。启动线程。。!!SynchronizedFile是一个类(为此任务提供给我们),它具有此对象的checkfilestate()功能。在服务器上,此类的对象处理JSON指令解析,在客户端,它处理这些指令的创建。另外,由于客户端和服务器线程类的性质,我需要将syncfile对象作为参数传递。至于你的问题,我还没有足够的经验来提供抽象概念。因此,使用紧密耦合的方法,添加缺少的Thread#start()语句。很抱歉。
    Thread sstt = new Thread(new SyncThreadServer(sfileList.entrySet().iterator().next().getValue(),clientSocket));
    sstt.start();
    for (Map.Entry<String, SynchronisedFile> entry : sfileList.entrySet())
    {
        Thread sstt = new Thread(new SyncThreadServer(entry.getValue(),clientSocket));
        sstt.setName(entry.getKey());
    }
public class SyncThreadServer implements Runnable {

SynchronisedFile toFile; // this would be on the Server //Is an instance of the syncfile class, should be able to proc insts
Socket clientSocket;
public SyncThreadServer(SynchronisedFile tf, Socket aClientSocket){
    toFile=tf;
    clientSocket = aClientSocket;

    }

@Override
public void run() {
    Instruction inst = null;
    InstructionFactory instFact=new InstructionFactory();      

    while(true){
 {    
        try{

        DataInputStream in = new DataInputStream(clientSocket.getInputStream());
        DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());

        String smsg = in.readUTF();

        Instruction receivedInst = instFact.FromJSON(smsg);
        System.err.println(smsg);
            // The Server processes the instruction
            toFile.ProcessInstruction(receivedInst);
            //if(receivedInst.Type().equals("EndUpdate")){
              //      out.writeUTF("NEXT"); //TODO: Change to Json
                //    out.flush();}
            //else
            //{
                out.writeUTF("GO"); //TODO: Change to Json
                out.flush();
            }
            //}

         catch (IOException e) {
            e.printStackTrace();
            System.exit(-1); // just die at the first sign of trouble
        } catch (BlockUnavailableException e) {
            // The server does not have the bytes referred to by the block hash.
            try {

                DataOutputStream out2 = new DataOutputStream(clientSocket.getOutputStream());
                DataInputStream in2 = new DataInputStream(clientSocket.getInputStream());
                out2.writeUTF("NGO");   //TODO: Change to Json
                out2.flush();


                String msg2 = in2.readUTF();
                Instruction receivedInst2 = instFact.FromJSON(msg2);
                toFile.ProcessInstruction(receivedInst2);
                if(receivedInst2.Type().equals("EndUpdate")){
                    out2.writeUTF("NEXT"); //TODO: Change to Json
                    out2.flush();}
            else
            {
                out2.writeUTF("GO"); //TODO: Change to Json
                out2.flush();
            }
            } catch (IOException e1) {
                e1.printStackTrace();
                System.exit(-1);
            } catch (BlockUnavailableException e1) {
                assert(false); // a NewBlockInstruction can never throw this exception
            }
        }
       // } //And here
    }

    } 
}