zoukankan      html  css  js  c++  java
  • 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什么的, 就嗝了.

    继续观察吧.

  • 相关阅读:
    Oracle spatial、openlayers、geoserver开发地理信息系统总结
    解决Geoserver请求跨域的几种思路,第二种思路用过
    OpenLayers中的球面墨卡托投影
    墨卡托投影、地理坐标系、地面分辨率、地图比例尺
    jQuery Easing 动画效果扩展
    jQuery实现鼠标移上弹出提示框,移出消失
    验证码生成组件--JCaptcha的使用
    jquery validate 验证
    Oracle查询错误分析:ORA-01791:不是SELECTed表达式
    启动tomcat报host-manager does not exist or is not a readable directory异常
  • 原文地址:https://www.cnblogs.com/Montauk/p/5826759.html
Copyright © 2011-2022 走看看