package com.pansoft.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Test4Client2 {
public static Object obj = new Object();
public static boolean flag = true;
public static boolean SendThread = true;
public static boolean readThread = true;
static Runnable runnable = null;
static boolean flagTask = true;
// 客服端的主入口
public static void main(String[] args) throws IOException {
start();
socketClient();
}
// 与服务器连通地址本机(127.0.0.1),局域网中其他机器是(服务器在局域网中的ip地址) 端口都是8888
public static void socketClient() throws IOException {
Socket socket = new Socket("127.0.0.1", 8888);
if (socket.isConnected()) {
// 如果连接成功了就开启写和读的进程
new writer1(socket).start();
new read1(socket).start();
} else {
System.out.println("服务器未开启");
}
}
//自动任务开启
public static void init() {
runnable = new Runnable() {
int i = 0;
@Override
public void run() {
try {
if(flagTask){
startws();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
service.scheduleAtFixedRate(runnable, 0, 10, TimeUnit.SECONDS);
}
//自动任务开启
public static void start() {
if(runnable == null){
init();
}
flagTask = true;
}
//自动任务关闭
public static void close() {
flagTask = false;
}
public static void startws() throws Exception{
System.out.println("************定时任务检测是否需要重启*************");
if(!readThread && !SendThread){
System.out.println("¥¥¥¥¥¥¥¥¥¥检测到任务正再重启¥¥¥¥¥¥¥¥¥¥¥");
socketClient();
}
}
}
// 写入到通道的线程
class writer1 extends Thread {
@SuppressWarnings("unused")
private Socket socket;
private PrintWriter printWriter;
public writer1(Socket socket) throws IOException {
this.socket = socket;
this.printWriter = new PrintWriter(socket.getOutputStream());
}
@Override
public void run() {
while (Test4Client2.flag) {
Test4Client2.SendThread = true;
printWriter.write("给我数据" + "\r\n");
printWriter.flush();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("发送端停止发送,重启连接!");
Test4Client2.flag = true;
Test4Client2.SendThread = false;
}
}
// 从通道中读取的线程
class read1 extends Thread {
private Socket socket;
private BufferedReader bufferedReader;
private String str = null;
public read1(Socket socket) throws IOException {
this.socket = socket;
this.bufferedReader = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
}
@Override
public void run() {
long start = System.currentTimeMillis();
long end = start;
while ((start - end) < 10000 && true) {
try {
start = System.currentTimeMillis();
Test4Client2.readThread = true;
System.out.println("================="+ "结束时间: "+end +"开始时间: "+ start);
str = bufferedReader.readLine();
end = System.currentTimeMillis();
System.out.println(str + "结束时间: "+end +"开始时间: "+ start);
} catch (IOException e) {
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("跳出了线程");
Test4Client2.flag=false;
Test4Client2.readThread = false;
}
}