请问如何控制一个线程里面的发送的网络请求?如果发送超过指定的时间而没有接受到响应,就断开这个连接及发送的请求, 重启开始连接并再次发送.如此循环往复.
这样的效果能否在一个线程里面完成?,该如何做?请高手说一下思路 . 谢谢.
这样的效果能否在一个线程里面完成?,该如何做?请高手说一下思路 . 谢谢.
解决方案 »
- java gui 图片切换
- 重写
- 讨论一下多态性
- JAVA 读数据库 和 读xml文件比,哪个性能好,快
- jbuilder 7.0 的注册号 谢谢
- 怎么吧一个字符串写到一个文件保存下来?先谢谢啦!在线等待!
- 询问如何实现在关闭窗口时播放一段动画,动画内容是逐渐变小的窗口
- 简单问题
- 关于校验的问题。
- 送分了:)如何将一些需要输出的东西写到ByteArrayOutputStream输出流中,如一个二维数组String[][]。
- 用jni完成一个dll后,其他java程序怎样调用?
- 在eclipse下调用dll文件出现错误java.lang.UnsatisfiedLinkError: displayHelloWorld
为什么要在一个线程内完成??这样效率多低啊
如果同时有1000个消息要发,难道要new一千个线程??
用布尔变量来控制。指定规定的时间没有响应就让线程挂起- wait,不是线程本身的wait,是对象的wait,如果需要重用线程就释放对象的线程列表notifyAll或者notify,最好用notifyAll。
代码框架:
//添加Runnable接口
isWait : boolean ; //是否暂停
................
//重写RUN方法
void run(){
if(isWait){ //如果isWait被设置为true就执行循环体,另外就wait //do more work.. }else
wait();
}//释放
public void XXXXX(){
对象.NotifyAll();
线程.start();
}
定时检测?好像很难实现啊.
我这个程序还是要写在j2me里面的. 发送请求只能写在另外一个线程.
{
String responseStr = null;
HttpConnection hpc = null;
DataInputStream dis = null;
boolean newline = false;
String content = "";
try {
// ===========================================================
// 建立连接
// ===========================================================
//hpc = (HttpConnection) Connector.open(request);
hpc = (HttpConnection) Connector.open(request); //手机代理
hpc.setRequestMethod(HttpConnection.GET);
// hpc.setRequestProperty("Connection", "close");
//hpc.setRequestProperty("Connection", "Keep-Alive");
hpc.setRequestProperty("accept","text/html, application/xhtml+xml, application/vnd.wap.xhtml+xml, text/vnd.wap.wml, application/vnd.wap.wmlc, application/vnd.wap.wbxml, application/vnd.wap.wmlscriptc, application/vnd.wap.multipart.mixed, multipart/mixed, text/x-vcard, text/x-vcalendar, audio/*, application/*, */*, image/gif, image/vnd.wap.wbmp");
hpc.setRequestProperty("cache-control","no-cache");
hpc.setRequestProperty("user-agent","SonyEricssonP908/R101 Profile/MIDP-2.0 Configuration/CLDC-1.0");
hpc.setRequestProperty("accept-language","zh, en");
hpc.setRequestProperty("accept-charset","utf-8, utf-16, iso-8859-1, *");
hpc.setRequestProperty("x-wap-profile","http://www.sonyericsson.com/UAProf/P900R101.xml");
hpc.setRequestProperty("X-Online-Host", host); //在手机测试需要
//dis = new DataInputStream(hpc.openInputStream());
dis = hpc.openDataInputStream();
int character;
// ===========================================================
// 读取返回的HTTP内容
// ===========================================================
while ((character = dis.read()) != -1) {
if ((char) character == '\\') {
newline = true;
continue;
} else {
if ((char) character == 'n' && newline) {
content += "\n";
newline = false;
} else if (newline) {
content += "\\" + (char) character;
newline = false;
} else {
content += (char) character;
newline = false;
}
}
}
} catch (IOException e) {
System.out.print("ERROR:" + e);
} finally {
if (hpc != null) {
try {
hpc.close();
} catch (Exception e) {
}
hpc = null;
}
if (dis != null) {
try {
dis.close();
} catch (Exception e) { } }
}
.........如何检测网络连接及发送耗费的时间?? 如果超过指定的一个时间,比如1分钟,就重新连接.
定时检测?好像很难实现啊.
我这个程序还是要写在j2me里面的. 发送请求只能写在另外一个线程.
------------------------------------
获取时间:
发送消息时获取当前时间:
long befor = System.currentTimeMills()
不管是成功或者失败都回返回的,在返回方法中获取当前时间:
long after = System.currentTimeMills();long delay = befor - after ;//超时 如果超时就让线程进行等待wait或者不活动join