我有使用多线程将对象发送到 ServerSocket 的代码(当前在本地,但将来在本地网络中)
用于发送对象:
public class SocketToAdapter {
public static void writeObject(Object object) {
    try {
        give().writeUnshared(object);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}
static ObjectOutputStream give() {
    Socket s = null;
    try {
        s = new Socket("localhost", 9990);
        s.setTcpNoDelay(true);
        return new ObjectOutputStream(s.getOutputStream());
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
主要方法:
SocketToAdapter soc = new SocketToAdapter();
    thread1.setSocket(soc);
    thread2.setSocket(soc);
    thread3.setSocket(soc);
    thread4.setSocket(soc);
    thread5.setSocket(soc);
    synchronized (valueExchanging) {
        synchronized (soc) {
            thread1.start();
            thread2.start();
            thread3.start();
            thread4.start();
            thread5.start();
        }
valueExchang 是一个对象,用于在线程之间交换数据。
从线程运行方法:
public void run() {
    try {
        while (true) {
            curr = new Object(pair, RandomUtil.getRandomExchange(),
                    RandomUtil.getRandomTurn());
            //not important Business Logic.
                            int v1 = valueExchanger.getExchangeInTread()+1;
            int v2 = valueExchanger.getExchangeInTread()-100;
            curr = new Object(pair, BigInteger.valueOf(v1),
                    BigInteger.valueOf(v2));
                            //
            SocketToAdapter.writeObject(curr);
            valueExchanger.setExchangeInTread(v1);
            Thread.sleep(0, 1);
        }
    } catch (InterruptedException iex) {
    }
}
这有效,但非常缓慢。可能是因为我每次需要时都会创建 Socket 和 ObjectOutputStream 。我尝试创建一个 Socket 和一个 OOS 并像这样使用它:
                   {
        Socket s = new Socket("localhost", 9990);
        ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); }
接着
oos.writeUnshared(object); 
oos.flush(); 
oos.writeUnshared(object);
但是如果我第二次尝试重用oos,我会得到软件导致连接中止:套接字写入错误。不管我使用多少线程。
我需要的是每秒发送许多(例如100k)对象的可能性,有什么建议吗?
在服务器端我做:
服务器.java:
    ServerSocket ss;
public static void pre()throws IOException, ClassNotFoundException {
    ss = new ServerSocket(9990);
    }
public static Object start() throws IOException, ClassNotFoundException {
    Object o = null;
    Socket s = ss.accept(); 
    while (!s.isClosed()) {
        ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
        o = (Object) ois.readObject();
        ois.close();
        s.close(); 
    }
    ss.close();
    return o;
}
“主要方法”
    while (true) { 
            try {
                Serwer.pre();
                Object o = Serwer.start();
                                    //im do somethink with that object o.
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }