嵌入式小渣渣 2021-10-09 20:59 采纳率: 0%
浏览 24

在linux服务器上运行几个小时就top显示虚拟内存很多,也变成了睡眠态,CPU占用都到了50%,不知道怎么回事,希望大家能帮帮忙啊



```java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

public class server {
    public static void main(String arg[]) {
        wangluo.w = new wangluo[5];
        byte len = (byte)wangluo.w.length;
        Socket s = null;
        ServerSocket ss = null;
        try {
           ss = new ServerSocket(8888);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("server is readying");
        byte i;
        while (true){
            for (i=0;;i++) {
                if (i == len) i = 0;
                if (wangluo.w[i]!=null) continue;
                if (wangluo.w[i]==null) break;
            }
            System.out.println(wangluo.sum+" 号位等待中......");
            try {
                s = ss.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
            wangluo.w[i] = new wangluo(i,s);
        }
    }
}
class wangluo extends Thread{
    public static byte sum;
    public static wangluo[] w;
    private boolean alive=false;
    private String name ="";
    private byte i;
    private Socket s = null;
    private InputStream input = null;
    private OutputStream output = null;
    public wangluo(byte i,Socket s) {
        this.i = i;
        this.s = s;
        try {
            input = s.getInputStream();
            output = s.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
      start();
    }
    @Override
    public void run() {
        alive = check();
        sum++;
        String data;
        while(alive){
            data = getdata();
            for (int j=0;j<w.length&&alive;j++){
                if (j!=i&&w[j]!=null&&w[j].alive) w[j].write(data.getBytes());
            }
        }
        sum--;
       
    }
    private boolean check(){
        boolean bool = true;
        FileInputStream f = null;
        name = getdata();
        String pass = getdata();
        try {
            f = new FileInputStream("server.properties");
            Properties p = new Properties();
            p.load(f);
            if(!pass.equals(p.getProperty(name))) bool = false;
            else write("ok".getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (f!=null) {
                try {
                    f.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bool;
    }
    private void close(){
        alive = false;
        try {
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            w[i] = null;
        }
    }
    public void write(byte[] i){
        try {
            output.write(i);
        } catch (IOException e) {
            close();
        }
    }
    public void write(byte i){
        try {
            output.write(i);
        } catch (IOException e) {
            close();
        }
    }
    private String getdata(){
        String data = "";
        byte dat = 0;
        try {
            while((dat = (byte) input.read())!=0){
                if(dat==-1) {
                    close();
                    break;
                }
                data = data+(char)dat;
            }
            write("ds".getBytes());
        } catch (IOException e) {
            close();
        }
        return data;
    }
}


  • 写回答

2条回答 默认 最新

  • 关注

    访问量有多大?保存网络用户的数据最好用hashtable,用完及时清理内存。

    评论

报告相同问题?

问题事件

  • 创建了问题 10月9日

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败