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

继续观察吧.

Java的多线程+Socket 后台 Ver 2.0的更多相关文章

  1. Java的多线程+Socket 后台

    打包好可执行的jar文件之后, 用ftp上传到服务器, 用nohup命令, 让这个服务器在后台运行, 并将输出重定向到log文件中, 命令是: #nohup java -jar MTSServer.j ...

  2. Java的多线程+Socket

    客户端: package com.wulala; import java.io.FileOutputStream;import java.io.IOException;import java.io.I ...

  3. Java多线程 Socket使用

    点我跳过黑哥的卑鄙广告行为,进入正文. Java多线程系列更新中~ 正式篇: Java多线程(一) 什么是线程 Java多线程(二)关于多线程的CPU密集型和IO密集型这件事 Java多线程(三)如何 ...

  4. Java 多线程 socket 取款例子 runnable callable

    socket部分参考 http://blog.csdn.net/kongxx/article/details/7259465 取款部分参考 http://blog.csdn.net/dayday198 ...

  5. Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed"

    刚开始使用IDEA 总是有各种各样奇葩的问题 启动报错: Unable to open debugger port (127.0.0.1:51554): java.net.SocketExceptio ...

  6. IDEA项目启动报Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed"

    启动报错: Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed&q ...

  7. IntelliJ IDEA 启动tomcat 报错: idea Unable to open debugger port (127.0.0.1:58233): java.net.SocketException "socket closed"

    debug启动项目弹出提示 Error running omp: Unable to open debugger port (127.0.0.1:50812): java.net.SocketExce ...

  8. Unable to open debugger port (127.0.0.1:63959): java.net.SocketException "socket closed",编译过来就是无法打开调试器端口,套接字已关闭

    最开始的预测: 这台笔记本操作系统是win10专业工作站版,debug启动项目,provide项目完美启动成功,然后consumer项目报错:Unable to open debugger port ...

  9. java程序报错:Unable to open debugger port (127.0.0.1:63959): java.net.SocketException "socket closed",编译过来就是无法打开调试器端口,套接字已关闭

    报错:Unable to open debugger port (127.0.0.1:63959): java.net.SocketException "socket closed" ...

随机推荐

  1. The Secrets of Oracle Row Chaining and Migration

    from http://www.akadia.com/services/ora_chained_rows.html Overview If you notice poor performance in ...

  2. NYU Hand Pose Dataset

    http://cims.nyu.edu/~tompson/NYU_Hand_Pose_Dataset.htm#overview

  3. 密码有效性验证失败。该密码不够复杂,不符合 Windows 策略要求

    我在sqlserver2005中建立一个用户的时候,我的用户名和密码是一样的,它不允许我建立报“密码有效性验证失败.该密码不够复杂,不符合 Windows 策略要求”错误,我把密码改成其他一些就可以, ...

  4. Oracle SQLserver数据库创建表ID字段的自动递增_序列

    Oracle 将表t_uaer的字段ID设置为自增:(用序列sequence的方法来实现) ----创建表 Create table t_user( Id ),userid ),loginpasswo ...

  5. DLL项目报错:fatal error lnk1104: cannot open file "...\xxx.dll"

    在生成DLL的时候报这个错, 生成不了DLL 检查生成DLL的路径是否有其他程序在使用... 或者把杀毒软件关了试试...

  6. iOS 导航控制器返回栈中的某一控制器

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  7. 使用Java创建RESTful Web Service

    REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移).2000年Roy Fielding博士在他的博士论文“Architectural Sty ...

  8. Idea 安装 lombok

    idea 目前是Java开发者最流行的一款编辑器.为了让java开发更加的简便idea 也提供了lombok的插件. 插件的按钮方式为: 1.进入idea的.setting面板 2.按照以下图进行操作 ...

  9. struts2 标签为简单标签

    <s:form method="post" action="" theme="simple"> <s:textfield ...

  10. CSS自定义弹出框

    <script type="text/javascript" language="javascript"> function sAlert(str) ...