今天貌似發(fā)現(xiàn)了一個java的bug.
這是一個內(nèi)部匿名類調(diào)用外部類方法的問題.
我在外部類中有個notifyAll(Object me)方法,在內(nèi)部匿名類里卻無法調(diào)用它,編譯報錯。
嘗試把notifyAll改名為tellAll,——恩,蠻好,可以調(diào)用。
嘗試寫成 OuterClassName.this.notifyAll(me)——恩,也可以調(diào)用。
看起來如果在外部類中重載Object的方法,java無法區(qū)分。

/**
?*?
?
*/

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;

/**
?*?任務(wù)狀態(tài)定時統(tǒng)計線程,只記錄發(fā)生了變動的統(tǒng)計結(jié)果.<br>
?*?以單例模式運(yùn)行,通過觀察者模式發(fā)送統(tǒng)計結(jié)果<br>
?*?典型的觀察者有客戶端通訊和統(tǒng)計曲線圖
?*?
?*?
@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();
????}


????
/**
?????*?啟動,以固定的延遲頻率統(tǒng)計數(shù)據(jù)庫中任務(wù)的狀態(tài),如果狀態(tài)發(fā)生變化則通知監(jiān)聽者,并記錄.
?????
*/

????
public ? void ?start()? {
????????
if ?(started)
????????????
return ;
????????
if ?(log.isInfoEnabled())
????????????log.info(
" 啟動任務(wù)統(tǒng)計線程 " );
????????started?
= ? true ;

????????
final ?StatisticDAO?dao? = ? new ?StatisticDAO();
????????schedule.scheduleWithFixedDelay(
????????????????
????????????????
new ?Runnable()? {
????????????????????TaskCountRecord?lastRecord?
= ? null ;
????????????????????
public ? void ?run()? {
????????????????????????TaskCountRecord?totalCount?
= ?dao.countTaskStatus();
????????????????????????
// ?任務(wù)數(shù)沒有變化則不更新
???????????????????????? if ?( ! totalCount.equals(lastRecord))? {
????????????????????????????lastRecord?
= ?totalCount;
????????????????????????????tellAllObservers(totalCount);
????????????????????????????dao.save(totalCount);?
????????????????????????????????notifyAll(totalCount);//該方法在父類中
????????????????????????}
?
????????????????????}

????????????????}
,
????????????????statisticRate,?
????????????????statisticRate,?
????????????????TimeUnit.SECONDS
????????);
????}


????
/**
?????*?結(jié)束
?????
*/

????
public ? void ?stop()? {
????????schedule.shutdown();
????????started?
= ? false ;
????????
if ?(log.isInfoEnabled())
????????????log.info(
" 停止任務(wù)統(tǒng)計線程 " );
????}

}