Java multi-threaded +socket background Ver 2.0

Source: Internet
Author: User
Tags getmessage

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 ());
}

}
}

}
Please disregard the following inner class.
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 is 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 ();
}
}
}

}
}

Separate the inherited classes of the process:

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..");
}

}

}

Re-multi-threaded look, now basically know how to monitor the situation of the thread, but still did not install the Java SDK on the Cent OS (company speed slow into a dog), so can not be used in the production of the server with Jstack monitoring, back to find a way to the JDK's RPM Daoteng to the production server up, Try monitoring.

Now mainly with Netstat and PS to see the state, PS see always no problem, process still in, netstat-an|grep 9999 see if is a bunch of wait_to_close what, on the belch.

Keep watching.

Java multi-threaded +socket background Ver 2.0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.