谢谢哥哥姐姐们
解决方案 »
- 求一些面试的编程题目。。。
- 寻找XML文件分割的工具
- 如何在ISO8859-1的控制台上显示GBK的中文?
- 请问怎样将JTable中的输入值更新至TableModel中
- 问大家一个小问题,java不是支持unicode吗,为什么不能显示出韩语!
- 刚才有人问了一个方阵的问题吧?怎么把帖子删掉了
- 我一直是作信息管理系统开发的,请问什么是嵌入式开发,举个例子?如何开发?用于哪类项目?
- 线程问题:3问题
- 为什么System.out.println(new DataInputstream.readShort())不是期望的值?
- 来来来,小白送分给大牛了 求助
- 一个数字区间计算的问题
- 搞不懂哦,这句话怎么会不对呢?
声明2点,1.使用setSoTimeout就可以做到下面的功能。2.对于同步,有些地方处理的不是很细致,仅做参考。import java.io.*;
import java.net.*;
import java.util.*;public class TestBlocking {
static class Receiver extends Thread {
private final int WAITING_TIME = 800;
private final int COUNT_ONCE = 3;
private Boolean terminate = false; Socket socket;
InetSocketAddress addr; Receiver(Sender serv) {
socket = new Socket();
addr =
new InetSocketAddress(serv.getPort());
}
class Monitor implements Runnable {
Thread me;
Boolean stop = false; public synchronized void wakeup() {
this.notify();
//System.out.printf("momi: wakeup, %b\n", terminate);
} public void start() {
me = new Thread(this);
//me.setPriority(Thread.MAX_PRIORITY);
me.start();
}
public synchronized void stop() {
stop = true;
this.notify();
}
public void run() {
while (true) {
try {
synchronized (this){
wait(WAITING_TIME * 2);
}
if (stop) {
break;
}
synchronized (this){
wait(WAITING_TIME);
}
if (stop) {
break;
}
synchronized(terminate) {
if (terminate) {
try {
socket.close();
//break;
} catch (IOException ioe) {
ioe.printStackTrace();
// } finally {
// socket = null;
// break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
}
}
public void run() {
int times = 0;
Monitor monitor = new Monitor();
while (true) {
try {
socket.connect(addr, 40);
} catch (SocketTimeoutException ste) {
if (times++ < 10) {
System.out.println("recv: where are you?");
}
continue;
} catch (IOException ioe) {
break;
}
} terminate = false;
monitor.start();
try {
InputStream in = socket.getInputStream();
//char[] buf = new char[50];
//int off = 0, len = COUNT_ONCE; System.out.println("recv: Hello");
while (true) {
int c;
System.out.print("recv heard: \n\t");
while (socket != null) {
synchronized(terminate) {
terminate = true;
}
monitor.wakeup(); c = in.read();
if (c == -1) {
break;
}
System.out.print((char)c);
synchronized(terminate) {
terminate = false;
monitor.wakeup();
}
} break;
} } catch (SocketException se) {
System.out.println(
"\nrecv: I can't bear to wart for so long time, bye!");
monitor.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
} static class Sender extends Thread {
private final int WAITING_TIME = 400;
private final int COUNT_ONCE = 5; private final String toSay =
"Hello, I love you, and you?"; private ServerSocket serv = null; Sender() throws IOException{
serv = new ServerSocket(0);
serv.setSoTimeout(50);
}
public InputStream getStream(){
return null;
}
public int getPort() {
return serv.getLocalPort();
} public void run() {
int times=0;
System.out.println("serv want to say: \n\t" + toSay);
while (true) {
try {
Socket socket = serv.accept(); PrintWriter out =
new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream())); InputStream in =
socket.getInputStream();
for (int i=0; i<toSay.length(); i+=COUNT_ONCE) {
int writeCount = toSay.length() - i;
writeCount = writeCount<=COUNT_ONCE ?
writeCount : COUNT_ONCE; String s = toSay.substring(i, i + writeCount);
out.write(s);
out.flush();
if (i<10) {
sleep(WAITING_TIME);
} else {
sleep(WAITING_TIME + 100);
}
} //out.close(); } catch (SocketTimeoutException ste) {
//ste.printStackTrace();
if (times++<3) {
System.out.println("serv: are you here?");
} else {
System.out.println("serv: oh, shit!");
System.out.println("\nUnagain: haha!");
break;
}
} catch (IOException ioe) {
ioe.printStackTrace();
break;
} catch (InterruptedException ire) {
ire.printStackTrace();
break;
}
} if (serv != null && !serv.isClosed()) {
try {
serv.close();
} catch (IOException e) {
} finally {
serv = null;
}
}
}
} public static void main(String[] args) {
try {
Sender sender = new Sender();
sender.start();
(new Receiver(sender)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private Boolean terminate = false;