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. 破解win10系统密码

  2. mongoose的promise(转发)

    Switching out callbacks with promises in Mongoose Published on July 28, 2015 mongo node mongoose pro ...

  3. node.js render模板

    在用node组织前端架构和后端的时候,如果不用nginx做反向代理,则会考虑怎么样render模板. 在现有的项目中没有以下几种方式render模板: 1.将.html当做静态文件,如果url定位到哪 ...

  4. 堆heap和栈Stack(百科)

    堆heap和栈Stack 在计算机领域,堆栈是一个不容忽视的概念,堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈 ...

  5. [g2o]一个备忘

    g2o使用的一个备忘 位姿已知,闭环的帧已知,进行图优化. #include "stdafx.h" #include <vector> #include "P ...

  6. iOS 黑屏

    1. 首先看操作系统是否有很多Bug 9.0.2 的系统不稳定. http://tech.163.com/15/1020/05/B6BL6PML000915BD.html

  7. 【转】轻量级分布式 RPC 框架

    第一步:编写服务接口 第二步:编写服务接口的实现类 第三步:配置服务端 第四步:启动服务器并发布服务 第五步:实现服务注册 第六步:实现 RPC 服务器 第七步:配置客户端 第八步:实现服务发现 第九 ...

  8. 全局函数VS成员函数

    #include <iostream> using namespace std; class Test { public: Test(int a, int b) { this->a ...

  9. android 添加背景音乐

    MediaPlayer mediaPlayer=MediaPlayer.create(MainActivity.this,R.raw.qiji); mediaPlayer.start();

  10. SNMP ber 编码

    5.1 标识域(tag)的编码规则 标识域指明数据的类型,占用1个字节,常见的类型有:BOOL(0x01);INT(0x02);OCTSTR(0x04);NULL(0x05);OBJID(0x06); ...