private class CheckThread extends Thread {
volatile boolean stop = false;
@Override
public void run() {
synchronized(this) {
try {
int count = 0;
while (!stop) {
if (DEBUG) Slog.v(TAG, "start checking stat ...");
checkServer();
sleep(1000 * 20);
count++;
if (count >= MAX_TRY_CHECK) {
if (DEBUG) Slog.v(TAG, "!!!reconnect reach max fail time, stop check!!!");
stop = true;
count = 0;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private CheckThread mThread = new CheckThread();
// 启动
if (!mThread.isAlive()) {
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
mThread.stop = false;
mThread.start();
if (DEBUG) Slog.v(TAG, "start check thread");
}
// 结束
if (mThread.isAlive()) {
if (DEBUG) Slog.v(TAG, "stop check thread");
mThread.stop = true;
mThread.interrupt();
}
/**
* 销毁线程方法1
*/
private void destroyThread() {
try {
isStart = false;
if (null != mRecordThread && Thread.State.RUNNABLE == mRecordThread.getState()) {
try {
Thread.sleep(500);
mRecordThread.interrupt();
} catch (Exception e) {
mRecordThread = null;
}
}
mRecordThread = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
mRecordThread = null;
}
}
// 方法二
if (mVThread.isAlive()) {
if (DEBUG) Slog.v(TAG, "stop v check thread");
mVThread.stop = true;
mVThread.interrupt();
}
//check connection
private class VCheckThread extends Thread {
volatile boolean stop = false;
@Override
public void run() {
synchronized(this) {
try {
int count = 0;
while (!stop) {
if (DEBUG) Slog.v(TAG, "start checking v stat ...");
checkServer();
sleep(1000 * 20);
count++;
if (count >= MAX_TRY_CHECK_V) {
if (DEBUG) Slog.v(TAG, "!!!reconnect reach max fail time, stop checking!!!");
stop = true;
count = 0;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}