0

我想通过以下代码将线程转换为 newFixedThreadpool() 我该怎么做?

//saddddddddddddddddddddddddddddffgfggggggggggggggggggggggggfdhfdhfdhdhdfhjhljgjhgdadsadsafds

班级客户:

 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.ObjectInputStream;  
 import java.io.ObjectOutputStream;  
 import java.net.Socket;  
 import java.util.Arrays;  
 import java.lang.*;  
 import java.util.Scanner;  



public class Client {  

public static void main(String[] args) throws Exception {  
    String fileName = null;  

   try {  
        fileName = args[0];  
    } catch (Exception e) {  
    System.out.println("Enter the name of the file :");  
    Scanner scanner = new Scanner(System.in);  
    String file_name = scanner.nextLine();  

    File file = new File(file_name);  
    Socket socket = new Socket("localhost", 3332);  
    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());  
    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());  

    oos.writeObject(file.getName());  

    FileInputStream fis = new FileInputStream(file);  
    byte [] buffer = new byte[Server.BUFFER_SIZE];  
    Integer bytesRead = 0;  

    while ((bytesRead = fis.read(buffer)) > 0) {  
        oos.writeObject(bytesRead);  
        oos.writeObject(Arrays.copyOf(buffer, buffer.length));  
    }  

    oos.close();  
    ois.close();  
    System.exit(0);      
   }  

   }  

   }

班级服务器:

 import java.io.FileOutputStream;  
 import java.io.*;
 import java.io.ObjectInputStream;  
 import java.io.ObjectOutputStream;  
 import java.net.ServerSocket;  
 import java.net.Socket;  

public class Server extends Thread {  
public static final int PORT = 3332;  
public static final int BUFFER_SIZE = 100;  


public void run() {  
    try {  
        ServerSocket serverSocket = new ServerSocket(PORT);  

        while (true) {  
            Socket s = serverSocket.accept();  
            saveFile(s); 
        }  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
}  

private void saveFile(Socket socket) throws Exception {  
    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());  
    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());  
    FileOutputStream fos = null;  
    byte [] buffer = new byte[BUFFER_SIZE];  


    Object o = ois.readObject();  

    if (o instanceof String) {  
        fos = new FileOutputStream(new File("/home/reza/Desktop/reza"));  
    } else {  
        throwException("Something is wrong");  
    }  


    Integer bytesRead = 0;  

    do {  
        o = ois.readObject();  

        if (!(o instanceof Integer)) {  
            throwException("Something is wrong");  
        }  

        bytesRead = (Integer)o;  

        o = ois.readObject();  

        if (!(o instanceof byte[])) {  
            throwException("Something is wrong");  
        }  

        buffer = (byte[])o;  


        fos.write(buffer, 0, bytesRead);  

    } while (bytesRead == BUFFER_SIZE);  

    System.out.println("File transfer success");  

    fos.close();  

    ois.close();  
    oos.close();  
}  

public static void throwException(String message) throws Exception {  
    throw new Exception(message);  
}  

public static void main(String[] args) {  

new Server().start();  
}  
}
4

1 回答 1

0
public class Server extends Thread {  
  public void run() {
    try {  
      ServerSocket serverSocket = new ServerSocket(PORT);  
      while (true) {  
        Socket s = serverSocket.accept();  
        new ServerWorker(s).start();
      }  
    } catch (Exception e) {  
      e.printStackTrace();  
    }
  }  
}  

public class ServerWorker extends Thread {
  private Socket _socket;

  public ServerWorker(Socket socket) {
    this._socket = socket;
  }

  public void run() {
    saveFile(this._socket);
    // Thread should terminate when run returns.
  }

  private void saveFile(Socket socket) throws Exception {  
    ...
  }
}
于 2013-03-07T05:52:11.700 回答