• 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什么的, 就嗝了.

    继续观察吧.

  • 相关阅读:
    对指定文件生成数字摘要的MD5工具类
    shell脚本学习积累笔记(第一篇)
    java项目打成jar包时引用了第三方jar,此时我们该如何解决呢
    分享关于学习new BufferedWriter()方法时常遇到的一个无厘头的问题
    WebService学习整理(一)——客户端三种调用方式整理
    TZOJ 挑战题库随机训练02
    TZOJ 挑战题库随机训练01
    TZOJ 2943 Running Median(动态中位数)
    TZOJ 3927 Circular Sequence(环形最大子段和)
    TZOJ 3698 GCD depth(数学)
  • 原文地址:https://www.cnblogs.com/Montauk/p/5826759.html
Copyright © 2020-2023  润新知