Re: a Timer in a URLConnection
- From: Marcelo <mache_1999@xxxxxxxxx>
- Date: Wed, 30 Nov 2005 01:11:16 +0100
Hi everybody,
Now i have something like a solution, but I need some help here. The only thing that doesn't work very well is the setDaemon() method. I don't want to use it because my program can still running if there are other active threads (which is possible) so it will take part of my Internet connection.
I don't know how to avoid this step, because the line that is driving me crazy is
length = connection.getContentLength();
somehow, the connection is still open even if I kill the thread t=null Do you have an idea in order to avoid this problem?
thanks for your help,
Marcelo
public class TimerTemp{
public Timer timer;
public int length = -1; public boolean isFinished = false;
URLConnection connection;
Thread t;
public TimerTemp(final URL urlObject){
t = new Thread(){
public void run(){
try{
connection = urlObject.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;MSIE 5.5; Windows NT 5.0;H010818)" );
//Connect to the server connection.connect();
length = connection.getContentLength();
}catch(IOException e){
e.printStackTrace();
}
}
};
//TODO: Not a good implementation
t.setDaemon(true);
t.start();
timer = new Timer(true);
timer.schedule(new temp(), 2000);
}
private class temp extends TimerTask{
public void run(){
if(length == -1)
System.out.println("Time's up");
else
System.out.println("length "+length);
timer.cancel();
connection = null;
System.out.println("alive:"+t.isAlive()+t.getState());
t = null;
isFinished= true; }
}
}
.
- References:
- a Timer in a URLConnection
- From: Marcelo
- a Timer in a URLConnection
- Prev by Date: Re: formatted printing of text- color
- Next by Date: Re: Java PreparedStatements vs ADODB.Command
- Previous by thread: Re: a Timer in a URLConnection
- Next by thread: formatted printing of text- color
- Index(es):
Relevant Pages
|