我想创建一个java类(线程),它可以ping twitter,如果没有连接,则等待连接并重新运行一些其他类和线程。 我有“ping”网站的代码,并且运行每个静态方法的方法都在我的Main类中。这是解决问题的好办法吗?在java中创建一个timeOut类
这里是代码的基本部分:
while (true){
try {
final URLConnection connection = new URL(url).openConnection();
connection.connect();
}
catch (Exception e) {
Thread.sleep(10000*t);
if (url.matches(twitter1)){
Thread method1= new Thread(Class1.method1);
method1.start();
}else if (url.matches(twitter2)){
Thread method2 = new Thread(Class1.method2);
method2.start();
}else if (url.matches(twitter3)){
Main.StaticMethod();
}else if (url.matches(twitter4)){
Main.StaticMethod2();
}else if (url.matches(twitter5)){
Main.StaticMethod3();
}else{
System.out.println("Unknown URL");
}
t=2^t;
}
}
[使用计时器](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html) – AJG85