今天貌似发现了一个java的bug.
这是一个内部匿名类调用外部类方法的问题.
我在外部类中有个notifyAll(Object me)方法,在内部匿名类里却无法调用它,编译报错。
尝试把notifyAll改名为tellAll,——恩,蛮好,可以调用。
尝试写成 OuterClassName.this.notifyAll(me)——恩,也可以调用。
看起来如果在外部类中重载Object的方法,java无法区分。
/** */
/**
*
*/
package cn.roob.webdown.statistic;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import cn.roob.webdown.persist.StatisticDAO;
import cn.roob.webdown.persist.TaskCountRecord;
import cn.roob.webdown.util.Config;
/** */ /**
* 任务状态定时统计线程,只记录发生了变动的统计结果.<br>
* 以单例模式运行,通过观察者模式发送统计结果<br>
* 典型的观察者有客户端通讯和统计曲线图
*
* @author tedeyang
*
*/
public class TaskStatistic extends CommonStatisticSource {
static private int statisticRate = Config.getApplicationConfig().statisticRate();
static private TaskStatistic thread = null ;
private static Logger log = Logger.getLogger(TaskStatistic. class );
private final ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
private boolean started = false ;
private TaskStatistic() {
}
public static TaskStatistic getSingleScheduledThread() {
if (thread == null ) {
thread = new TaskStatistic();
}
return thread;
}
/** */ /**
* 在垃圾收集前停止线程
* @see java.lang.Object#finalize()
*/
protected void finalize() throws Throwable {
this .stop();
super .finalize();
}
/** */ /**
* 启动,以固定的延迟频率统计数据库中任务的状态,如果状态发生变化则通知监听者,并记录.
*/
public void start() {
if (started)
return ;
if (log.isInfoEnabled())
log.info( " 启动任务统计线程 " );
started = true ;
final StatisticDAO dao = new StatisticDAO();
schedule.scheduleWithFixedDelay(
new Runnable() {
TaskCountRecord lastRecord = null ;
public void run() {
TaskCountRecord totalCount = dao.countTaskStatus();
// 任务数没有变化则不更新
if ( ! totalCount.equals(lastRecord)) {
lastRecord = totalCount;
tellAllObservers(totalCount);
dao.save(totalCount);
notifyAll(totalCount);//该方法在父类中
}
}
} ,
statisticRate,
statisticRate,
TimeUnit.SECONDS
);
}
/** */ /**
* 结束
*/
public void stop() {
schedule.shutdown();
started = false ;
if (log.isInfoEnabled())
log.info( " 停止任务统计线程 " );
}
}
*
*/
package cn.roob.webdown.statistic;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import cn.roob.webdown.persist.StatisticDAO;
import cn.roob.webdown.persist.TaskCountRecord;
import cn.roob.webdown.util.Config;
/** */ /**
* 任务状态定时统计线程,只记录发生了变动的统计结果.<br>
* 以单例模式运行,通过观察者模式发送统计结果<br>
* 典型的观察者有客户端通讯和统计曲线图
*
* @author tedeyang
*
*/
public class TaskStatistic extends CommonStatisticSource {
static private int statisticRate = Config.getApplicationConfig().statisticRate();
static private TaskStatistic thread = null ;
private static Logger log = Logger.getLogger(TaskStatistic. class );
private final ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
private boolean started = false ;
private TaskStatistic() {
}
public static TaskStatistic getSingleScheduledThread() {
if (thread == null ) {
thread = new TaskStatistic();
}
return thread;
}
/** */ /**
* 在垃圾收集前停止线程
* @see java.lang.Object#finalize()
*/
protected void finalize() throws Throwable {
this .stop();
super .finalize();
}
/** */ /**
* 启动,以固定的延迟频率统计数据库中任务的状态,如果状态发生变化则通知监听者,并记录.
*/
public void start() {
if (started)
return ;
if (log.isInfoEnabled())
log.info( " 启动任务统计线程 " );
started = true ;
final StatisticDAO dao = new StatisticDAO();
schedule.scheduleWithFixedDelay(
new Runnable() {
TaskCountRecord lastRecord = null ;
public void run() {
TaskCountRecord totalCount = dao.countTaskStatus();
// 任务数没有变化则不更新
if ( ! totalCount.equals(lastRecord)) {
lastRecord = totalCount;
tellAllObservers(totalCount);
dao.save(totalCount);
notifyAll(totalCount);//该方法在父类中
}
}
} ,
statisticRate,
statisticRate,
TimeUnit.SECONDS
);
}
/** */ /**
* 结束
*/
public void stop() {
schedule.shutdown();
started = false ;
if (log.isInfoEnabled())
log.info( " 停止任务统计线程 " );
}
}