如何解決Java Socket通信技術收發線程互斥
Java Socket通信技術在很長的`時間裏都在使用,在不少的程序員眼中都有很多高的評價。那麼下面我們就看看如何才能掌握這門複雜的編程語言,希望大家在今後的Java Socket通信技術使用中有所收穫。
下面就是Java Socket通信技術在解決收發線程互斥的代碼介紹。
age ;
rt ception;
rt tStream;
rt utStream;
rt SocketAddress;
rt et;
rt etException;
rt etTimeoutException;
rt leDateFormat;
rt ;
rt erties;
rt r;
rt rTask;
rt urrentHashMap;
rt Unit;
rt ition;
rt trantLock;
rt er;
19./**
20.*
title: socket通信包裝類
21.*
Description:
22.*
CopyRight: CopyRight (c) 2009
23.*
Company:
24.*
Create date: 2009-10-14
25.*author sunnylocus
26. * v0.10 2009-10-14 初類
27.* v0.11 2009-11-12 對命令收發邏輯及收發線程互斥機制進行了優化,
處理命令速度由原來8~16個/秒提高到25~32個/秒
28.*/ public class SocketConnection {
ate volatile Socket socket;
ate int timeout = 1000*10; //超時時間,初始值10秒
ate boolean isLaunchHeartcheck = false;//是否已啟動心跳檢測
ate boolean isNetworkConnect = false; //網絡是否已連接
ate static String host = "";
ate static int port;
ic InputStream inStream = null;
ic OutputStream outStream = null;
ate static Logger log =ogger
(s);
ate static SocketConnection socketConnection = null;
ate static r heartTimer=null;
40.//private final Map
synchronizedMap(new HashMap
ate final ConcurrentHashMap
= new ConcurrentHashMap
ate static Thread receiveThread = null;
ate final ReentrantLock lock = new ReentrantLock();
ate SocketConnection(){
erties conf = new Properties();
{
(esourceAsStream
(""));
out = eOf(roperty("timeout"));
(roperty("ip"),eOf
(roperty("port")));
50.} catch(IOException e) {
l("socket初始化異常!",e);
w new RuntimeException("socket初始化異常,請檢查配置參數");
53.}
54.}
55./**
56.* 單態模式
57.*/
ic static SocketConnection getInstance() {
(socketConnection==null) {
hronized(s) {
(socketConnection==null) {
etConnection = new SocketConnection();
rn socketConnection;
64.}
65.}
66.}
rn socketConnection;
68.}
ate void init(String host,int port) throws IOException {
SocketAddress addr = new InetSocketAddress(host,port);
et = new Socket();
hronized (this) {
("【準備與"+addr+"建立連接】");
ect(addr, timeout);
("【與"+addr+"連接已建立】");
ream = nputStream();
tream = utputStream();
cpNoDelay(true);//數據不作緩衝,立即發送
oLinger(true, 0);//socket關閉時,立即釋放資源
eepAlive(true);
rafficClass(0x04|0x10);//高可靠性和最小延遲傳輸
tworkConnect=true;
iveThread = new Thread(new ReceiveWorker());
t();
=host;
=port;
(!isLaunchHeartcheck)
chHeartcheck();
89.}
90.}
91./**
92.* 心跳包檢測
93.*/
ate void launchHeartcheck() {
(socket == null)
w new IllegalStateException("socket is not
established!");
tTimer = new Timer();
unchHeartcheck = true;
dule(new TimerTask() {
ic void run() {
ng msgStreamNo = treamNo("kq");
mstType =9999;//999-心跳包請求
leDateFormat dateformate = new SimpleDateFormat
("yyyyMMddHHmmss");
ng msgDateTime = at(new Date());
msgLength =38;//消息頭長度
ng commandstr = "00" +msgLength + mstType + msgStreamNo;
("心跳檢測包 -> IVR "+commandstr);
reconnCounter = 1;
e(true) {
ng responseMsg =null;
{
onseMsg = readReqMsg(commandstr);
113.} catch (IOException e) {
r("IO流異常",e);
nnCounter ++;
116.}
(responseMsg!=null) {
("心跳響應包 <- IVR "+responseMsg);
nnCounter = 1;
k;
121.} else {
nnCounter ++;
123.}
(reconnCounter >3) {//重連次數已達三次,判定網絡連接中斷,
重新建立連接。連接未被建立時不釋放鎖
nnectToCTCC(); break;
126.}
127.}
128.}
129.},1000 * 60*1,1000*60*2);
130.}
131./**
132.* 重連與目標IP建立重連
133.*/
ate void reConnectToCTCC() {
Thread(new Runnable(){
ic void run(){
("重新建立與"+host+":"+port+"的連接");
138.//清理工作,中斷計時器,中斷接收線程,恢復初始變量
el();
unchHeartcheck=false;
tworkConnect = false;
rrupt();
{
e();
145.} catch (IOException e1) {r("重連時,關閉socket連
接發生IO流異常",e1);}
146.//----------------
hronized(this){
(; ;){
{
entThread();
p(1000 * 1);
(host,port);
fyAll();
k ;
155.} catch (IOException e) {
r("重新建立連接未成功",e);
157.} catch (InterruptedException e){
r("重連線程中斷",e);
159.}
160.}
161.}
162.}
163.})t();
164.}
165./**
166.* 發送命令並接受響應
167.* @param requestMsg
168.* @return
169.* @throws SocketTimeoutException
170.* @throws IOException
171.*/
ic String readReqMsg(String requestMsg) throws IOException {
(requestMsg ==null) {
rn null;
175.}
(!isNetworkConnect) {
hronized(this){
{
(1000*5); //等待5秒,如果網絡還沒有恢復,拋出IO流異常
(!isNetworkConnect) {
w new IOException("網絡連接中斷!");
182.}
183.} catch (InterruptedException e) {
r("發送線程中斷",e);
185.}
186.}
187.}
ng msgNo = tring(8, 8 + 24);//讀取流水號
tream = utputStream();
e(ytes());
h();
ition msglock = ondition(); //消息鎖
193.//註冊等待接收消息
(msgNo, msglock);
{
();
t(timeout,ISECONDS);
198.} catch (InterruptedException e) {
r("發送線程中斷",e);
200.} finally {
ck();
202.}
ct respMsg = ve(msgNo); //響應信息
(respMsg!=null &&(respMsg != msglock)) {
205.//已經接收到消息,註銷等待,成功返回消息
rn (String) respMsg;
207.} else {
r(msgNo+" 超時,未收到響應消息");
w new SocketTimeoutException(msgNo+" 超時,未收到響應消息");
210.}
211.}
ic void finalize() {
(socket != null) {
{
e();
216.} catch (IOException e) {
tStackTrace();
218.}
219.}
220.}
221.//消息接收線程
ate class ReceiveWorker implements Runnable {
ng intStr= null;
ic void run() {
e(!rrupted()){
{
[] headBytes = new byte[4];
((headBytes)==-1){
("讀到流未尾,對方已關閉流!");
nnectToCTCC();//讀到流未尾,對方已關閉流
rn;
232.}
[] tmp =new byte[4];
= headBytes;
ng tempStr = new String(tmp)();
(tempStr==null || ls("")) {
r("received message is null");
inue;
239.}
tr = new String(tmp);
totalLength =eInt(intStr);
242.//----------------
[] msgBytes = new byte[totalLength-4];
(msgBytes);
ng resultMsg = new String(headBytes)+ new
String(msgBytes);
246.//抽出消息ID
ng msgNo = tring(8, 8 + 24);
ition msglock =(Condition) (msgNo);
(msglock ==null) {
(msgNo+"序號可能已被註銷!響應消息丟棄");
ve(msgNo);
inue;
253.}
(msgNo, resultMsg);
{
();
alAll();
258.}finally {
ck();
260.}
261.}catch(SocketException e){
r("服務端關閉socket",e);
nnectToCTCC();
264.} catch(IOException e) {
r("接收線程讀取響應數據時發生IO流異常",e);
266.} catch(NumberFormatException e){
r("收到沒良心包,String轉int異常,異常字符:"+intStr);
268.}
269.}
270.}
271.}
272.}
-
合格Sun認證Java程序員(SCJP)具備的能力
一個合格的Sun認證Java程序員(SCJP)需要具備以下能力:使用Java編程語言創建Java應用程序和applets;定義和描述垃圾蒐集,安全性和Java虛擬機(JVM);描述和使用Java語言面向對象的特點;開發圖形用户接口(GUI),利用Java支持的多種佈局管理;描述和使用Java1.1的事件處...
-
sun認證java關於字符串處理技巧
在sun認證考試中,java語言關於字符串處理技巧有哪些呢?下面yjbys小編跟大家分享字符串的一些處理方法如下:*@版本1.0*/publicclassStringUtil{publicStringUtil(){}/***@description字符串替代函數*@paramsourceString源字符串*@parambe_rep_strString需要被替換...
-
Solaris網絡管理認證考試須知
為了測試使用者對於Solaris網絡管理能力,Sun推出CertifiedNetworkAdministrator(CNA)。下面是小編整理的關於Solaris網絡管理認證考試須知,歡迎大家參考!為了測試使用者對於Solaris網絡管理能力,Sun推出CertifiedNetworkAdministrator(CAN)。內容包括基本網絡概念...
-
JAVA垃圾收集算法與內存泄露的解決方法
對於垃圾收集算法與內存泄露的問題,下面YJBYS小編為大家整理了關於JAVA垃圾收集算法與內存泄露解決方法,希望對你有所幫助。1.垃圾收集算法的核心思想Java語言建立了垃圾收集機制,用以跟蹤正在使用的對象和發現並回收不再使用(引用)的對象。該機制可以有效防範動...