今天貌似發現了一個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(
" 停止任務統計線程 " );
????}

}