当前位置: 技术问答>java相关
如何断开一个URLConnection或者HTTPURLConnection不让超时连接发生?有人说把他们放到线程里面,那么如何强迫杀死该线程?
来源: 互联网 发布时间:2015-01-26
本文导语: 如何断开一个URLConnection或者HTTPURLConnection不让超时连接发生?超时对软件效率有很大影响,所以如果访问到一些UnknownHost,必须把java.net.UnknownHostException出来以前断掉连接,有人说把他们放到线程里面,那么如何强迫杀...
如何断开一个URLConnection或者HTTPURLConnection不让超时连接发生?超时对软件效率有很大影响,所以如果访问到一些UnknownHost,必须把java.net.UnknownHostException出来以前断掉连接,有人说把他们放到线程里面,那么如何强迫杀死该线程?
|
import java.util.*;
import java.net.*;
import java.io.*;
public class TimedUrlConnection implements Observer {
private URLConnection ucon = null;
private int time = 300000;//max time out
private boolean connected = false;
public TimedUrlConnection (URLConnection ucon,int time) {
this.ucon = ucon;
this.time = time;
}
public boolean connect() {
ObservableURLConnection ouc = new
ObservableURLConnection(ucon);
ouc.addObserver(this);
Thread ouct = new Thread(ouc);
ouct.start();
try {
ouct.join(time);
}
catch (InterruptedException i) {
//false, but should already be false
}
return(connected);
}
public void update(Observable o, Object arg) {
connected = true;
}//end of public void update(Observable o, Object arg)
}
class ObservableURLConnection extends Observable implements Runnable {
private URLConnection ucon;
public ObservableURLConnection(URLConnection ucon) {
this.ucon = ucon;
}//end of constructor
public void run() {
try {
ucon.connect();
setChanged();
notifyObservers();
}
catch (IOException e) {
}
}//end of public void run()
}
//~~~~~~~~~~~~~~~~~~~Usage~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
URL url=new URL(/tech-qa-java/someurlname/index.html);
URLConnection urlconn=url.openConnection();
TimedUrlConnection timeoutconn=new TimedUrlConnection(urlconn,100000);//time out: 100seconds
boolean bconnectok=timeoutconn.connect();
if(bconnectok==false)
{
//urlconn fails to connect in 100seconds
}
else
{
//connect ok
}
import java.net.*;
import java.io.*;
public class TimedUrlConnection implements Observer {
private URLConnection ucon = null;
private int time = 300000;//max time out
private boolean connected = false;
public TimedUrlConnection (URLConnection ucon,int time) {
this.ucon = ucon;
this.time = time;
}
public boolean connect() {
ObservableURLConnection ouc = new
ObservableURLConnection(ucon);
ouc.addObserver(this);
Thread ouct = new Thread(ouc);
ouct.start();
try {
ouct.join(time);
}
catch (InterruptedException i) {
//false, but should already be false
}
return(connected);
}
public void update(Observable o, Object arg) {
connected = true;
}//end of public void update(Observable o, Object arg)
}
class ObservableURLConnection extends Observable implements Runnable {
private URLConnection ucon;
public ObservableURLConnection(URLConnection ucon) {
this.ucon = ucon;
}//end of constructor
public void run() {
try {
ucon.connect();
setChanged();
notifyObservers();
}
catch (IOException e) {
}
}//end of public void run()
}
//~~~~~~~~~~~~~~~~~~~Usage~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
URL url=new URL(/tech-qa-java/someurlname/index.html);
URLConnection urlconn=url.openConnection();
TimedUrlConnection timeoutconn=new TimedUrlConnection(urlconn,100000);//time out: 100seconds
boolean bconnectok=timeoutconn.connect();
if(bconnectok==false)
{
//urlconn fails to connect in 100seconds
}
else
{
//connect ok
}