【发布时间】:2013-09-13 07:12:08
【问题描述】:
代码如下:
timer.schedule(new TimerTask()
{
public void run()
{
synchronized(this)
{
try
{
// System.out.println(" ITERATION = ");
pachubeCLI.update(78164);
}
catch (PachubeException e)
{
// If an exception occurs it will print the error message from the
// failed HTTP command
System.err.println(e.errorMessage);
}
catch (IOException e)
{
System.err.println(e);
}
}
}
}, 0, 5*1000);
我可以看出,代码基本上是用于使用Timer 类的对象来安排操作。根据eclipse,传递给schedule方法的参数是(TimerTask task,long delay, long period)。但是看看这段代码,整个代码块被作为第一个参数传递,而不是对TimerTask 类的引用。我以前从未见过这样的方法。这里到底发生了什么?
一些背景知识:Timer 对象的 schedule 方法被用于定期更新 Xively(以前是 COSM(以前是 pachube))上的提要。
另外我不知道哪个标签描述了这里发生的事情。如果你知道,请添加它或发表评论。
【问题讨论】:
-
@JarrodRoberson 在stormehawke的回答之后我才知道它与anonymous-class有关。后来添加了标签
-
这里同步语句没用。写这篇文章的人不明白他/她在做什么......
标签: java anonymous-class