Java的多线程+Socket 后台 Ver 2.0

package com.wulala;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class JavaThreadCreationAndRun {

    public static void main(String[] args) {

        JavaThreadCreationAndRun jtca = new JavaThreadCreationAndRun();
        jtca.startServer();

    }

    public void startServer() {
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(9999);
        } catch (IOException e) {
            e.printStackTrace();
        }
        UpdateMySQL updateMySQL;
        while (true) {
            Socket socket = null;
            try {
                socket = ss.accept();
                updateMySQL = new UpdateMySQL(socket);
                Thread thread = new Thread(updateMySQL);
                thread.start();

            } catch (IOException e1) {
                System.out.println("client disconnected");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("close socket false: " + e.getMessage());
                }

            }
        }

    }
    //请无视下面这个内部类.
    static class Helper implements Runnable {
        private final String message;

        public Helper(String _message) {
            this.message = _message;
        }

        private void doSomething(String meesage) {
            System.out.println("The doSomethig method was executed by thread:" + Thread.currentThread().getName());
            System.out.println("Do something with " + message);
        }

        @Override
        public void run() {
            for (int i = 0; i < 2; i++) {
                doSomething(message);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

把进程的继承类独立出来了:

package com.wulala;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class UpdateMySQL extends Thread {
    private InputStream is = null;
    byte b[] = new byte[1024];
    int readCount = 0;
    ExecuteDML edml;

    public UpdateMySQL(Socket socket) {
        edml = new ExecuteDML();
        try {
            edml.initParam("dbconfig.properties");
        } catch (Exception e2) {
            System.out.println("init deml fail: " + e2.getMessage());
        }
        try {
            is = socket.getInputStream();
        } catch (IOException e1) {
            System.out.println("getInputStream exception: " + e1.getMessage());
        }
        try {
            readCount = is.read(b);
            System.out.println("readCount is " + readCount);

        } catch (IOException e1) {
            System.out.println("readCounter fail: " + e1.getMessage());
        } catch (Exception e) {
            try {
                is.close();
            } catch (IOException e1) {
                System.out.println("is close exeption: " + e1.getMessage());
            }
            // pw.close();
            try {
                socket.close();
            } catch (IOException e1) {
                System.out.println("socket close exeption: " + e1.getMessage());
            }
            System.out.println("socket exeption: " + e.getMessage());
        }

    }

    @Override
    public void run() {
        String str;
        str = new String(b);
        str = str.trim();
        System.out.println("Client Socket Message:" + str);

        String deviceID = "";
        int activate = 0;
        if (str.length() > 8 && (str.length() < 15)) {
            int insertResult = 0;
            // ExecuteDML edml = new ExecuteDML();
            deviceID = str.substring(0, 8);
            activate = Integer.valueOf(str.substring(8));
            try {
                insertResult = edml.insertWXData(deviceID, activate);
            } catch (Exception e) {
                System.out.println("insert problem" + e.getMessage());
            }

            // System.out.println("deviceID: " + deviceID + " activate: " +
            // activate);
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println("sleep problem..");
        }

    }

}

 

重新把多线程看了一下, 现在基本知道怎么监控线程的情况了, 可惜还是没有在Cent OS上面安装Java SDK(公司网速慢成狗), 所以没法在生产用服务器上用jstack监控, 回头想办法把jdk的rpm倒腾到生产服务器上去, 试试监控.

现在主要是用netstat跟ps看状态了, ps看到的总是没问题的, 进程还在, netstat -an|grep 9999看到的如果是一堆的WAIT_TO_CLOSE什么的, 就嗝了.

 

继续观察吧.

posted @ 2016-08-31 17:08  Montauk  阅读(247)  评论(0编辑  收藏  举报