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什么的, 就嗝了.
继续观察吧.
Java的多线程+Socket 后台 Ver 2.0的更多相关文章
- Java的多线程+Socket 后台
打包好可执行的jar文件之后, 用ftp上传到服务器, 用nohup命令, 让这个服务器在后台运行, 并将输出重定向到log文件中, 命令是: #nohup java -jar MTSServer.j ...
- Java的多线程+Socket
客户端: package com.wulala; import java.io.FileOutputStream;import java.io.IOException;import java.io.I ...
- Java多线程 Socket使用
点我跳过黑哥的卑鄙广告行为,进入正文. Java多线程系列更新中~ 正式篇: Java多线程(一) 什么是线程 Java多线程(二)关于多线程的CPU密集型和IO密集型这件事 Java多线程(三)如何 ...
- Java 多线程 socket 取款例子 runnable callable
socket部分参考 http://blog.csdn.net/kongxx/article/details/7259465 取款部分参考 http://blog.csdn.net/dayday198 ...
- 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 ...
- 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 ...
- 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 ...
- Unable to open debugger port (127.0.0.1:63959): java.net.SocketException "socket closed",编译过来就是无法打开调试器端口,套接字已关闭
最开始的预测: 这台笔记本操作系统是win10专业工作站版,debug启动项目,provide项目完美启动成功,然后consumer项目报错:Unable to open debugger port ...
- 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" ...
随机推荐
- androidSDK配置环境变量
android的开发人员来说,首先要做的就是环境变量的配置.java是需要配置环境变量的.当然,安卓的环境变量需要我们配置adb的使用,将开发平台的两个工具包配置到环境变量里. 工具/原料 and ...
- ZOOKEEPER解惑
现在网上关于ZooKeeper的文章很多,有介绍Leader选举算法的,有介绍ZooKeeper Server内部原理的,还有介绍ZooKeeper Client的.本文不打算再写类似的内容,而专注与 ...
- Python基础 (yield生成器)
如果在一个函数中使用了yield,那么这个函数实际上生成的是一个生成器函数 ,返回的是一个generator object.生成器是实现迭代的一种方式 特点: 其实返回的就是可以的迭代对象 和迭代的方 ...
- Windows 7 / Windows 10 安装 IPX/SPX
以我的系统为例: Windows 7/10 x64 首先下载 NWLINK IPX/SPX 驱动(这是 Microsoft 对 IPX/SPX 的实现.) http://pan.baidu.com/s ...
- 查找g++文档的方法
http://www.gnu.org/ -> Software(http://www.gnu.org/software/software.html) ->搜索 "gcc" ...
- 如何激活一个window/dialog && 不能直接对Dialog Box使用SetFocus
问题,症状: 程序的主窗口CMainWnd创建了一个modal dialog,希望这个dialog能接收WM_KEYDOWN消息,但是需要点一下这个dialog窗口它才能接收到(我嫌麻烦),而且我发现 ...
- iOS 归档
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- 使用Jquery+EasyUI 进行框架项目开发案例讲解之三---角色管理源码分享
使用Jquery+EasyUI 进行框架项目开发案例讲解之三 角色管理源码分享 在上两篇文章 <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享> ...
- Windows2000安装Winform Clickonce提示升级系统版本的解决方案
Windows2000安装Winform Clickonce提示升级系统版本.只需要把所有应用的DLL的独立性设置为false就可以了.
- C# ?和??运算表达式
1.单问号(?) 作用:用于给变量设初化的时候,给变量(int类型)赋为null值,而不是0. 例子: public int a; //默认值为0 public int ?b; //默认值为null ...