1
    abstract public class DSTimer {
    boolean iTmID;
    long m_lTime;
    Lock m_Lock = new Lock();

    boolean bLoop = false;
    static long mTimeZoneDiff = 0;

    private Handler mHandler = new Handler();

    public DSTimer() {
        iTmID = false;
        bLoop = false;
        mHandler.removeCallbacks(mUpdateTimeTask);
    }

    public void Destroy() {
        KillTimer();
    }

    abstract public void TimerProc();

    public static long GetDateTime() {
        return (((new Date()).getTime() / 1000) - mTimeZoneDiff);
    }

    public static long GetWorkTime() {
        return GetDateTime() % Constants.DAY_SECONDS;
    }

    static long m_dwWorkStartTime;
    static long m_dwPlayStartTime;

    public static void InitTime() {
        mTimeZoneDiff = CDSVSettings.getTimeZoneDiff() * 60;
    }

    public static void SetTimeZoneDiff (long ldiff) {
        mTimeZoneDiff = ldiff * 60; 
    }

    static long DateTimeToWorkTime(long llDateTime) {
        return 0;
    }

    static long WorkTimeToDateTime(long llWorkTime) {
        return 0;
    }

    public boolean SetTimer(long lElapse) {
        m_Lock.getLock();

        if (iTmID)
            mHandler.removeCallbacks(mUpdateTimeTask);

        m_lTime = lElapse;

        long lTime = Math.min(1000, lElapse) * 1000;
        if (lElapse <= 0)
            lTime = 1;

        iTmID = mHandler.postDelayed(mUpdateTimeTask, lTime);

        m_Lock.unLock();
        return true;
    }

    private Runnable mUpdateTimeTask = new Runnable() {
        public void run() {

        m_Lock.getLock();

            if (!bLoop) {

                bLoop = true;
            }

            m_lTime -= 1000;

            if (m_lTime <= 0) {
                iTmID = false;

                m_Lock.unLock();

                TimerProc();
                return;
            } else {
                m_Lock.unLock();

                ResetTimer();
            }

        }
    };

    public boolean ResetTimer() {
        long lTime = Math.min(1000, m_lTime) * 1000;
        if (lTime <= 0)
            lTime = 1;

        iTmID = mHandler.postDelayed(mUpdateTimeTask, lTime);

        return iTmID;
    }

    public boolean KillTimer() {
        m_Lock.getLock();
        if (iTmID) {
            iTmID = false;
            mHandler.removeCallbacks(mUpdateTimeTask);
        }

        m_Lock.unLock();
        return true;
    }

    public boolean IsActive() {
        return (iTmID);
    }

}

上に与えられているのはアンドロイドで書かれたコードです。純粋なJavaに変更する必要があります。以下のコードを試しましたが、illegalThreadstateexceptionエラーが表示されます。

 abstract public class DSTimer {
    boolean iTmID;
    long m_lTime;

    static boolean flagTimer;
    Lock m_Lock = new Lock();

    boolean bLoop = false;
    static long mTimeZoneDiff = 0;



    public DSTimer() {
        iTmID = false;
        bLoop = false;




        mUpdateTimeTaskThread.stop();

    }

    public void Destroy() {
        KillTimer();
    }

    abstract public void TimerProc();

    public static long GetDateTime() {
        return (((new Date()).getTime() / 1000) - mTimeZoneDiff);
    }

    public static long GetWorkTime() {
        return GetDateTime() % Constants.DAY_SECONDS;
    }

    static long m_dwWorkStartTime;
    static long m_dwPlayStartTime;

    public static void InitTime() {
        mTimeZoneDiff = CDSVSettings.getTimeZoneDiff() * 60;
    }

    public static void SetTimeZoneDiff (long ldiff) {
        mTimeZoneDiff = ldiff * 60; 
    }

    static long DateTimeToWorkTime(long llDateTime) {
        return 0;
    }

    static long WorkTimeToDateTime(long llWorkTime) {
        return 0;
    }

    public boolean SetTimer(long lElapse) {
        System.out.println("SetTimer() in DSTimer");
        m_Lock.getLock();

        if (iTmID)
        {



            mUpdateTimeTaskThread.stop();
        }
        m_lTime = lElapse;


        long lTime = Math.min(1000, lElapse) * 1000;
        if (lElapse <= 0)
            lTime = 1;


        long testTime=0;
        while(testTime<=lTime)
        {
            if(testTime>=lTime)
            {
                if(!mUpdateTimeTaskThread.isAlive())
            mUpdateTimeTaskThread.start();
            }
            else
             testTime++;
        }

        m_Lock.unLock();
        return true;
    }



    Thread mUpdateTimeTaskThread = new Thread("mUpdateTimeTask") {

        @Override
    public void run() {

        System.out.println("mUpdateTimeTaskThread in DSTimer");
        if(flagTimer==true)
        {

        m_Lock.getLock();

        if (!bLoop) {

            bLoop = true;
        }

        m_lTime -= 1000;

        if (m_lTime <= 0) {
            iTmID = false;

            m_Lock.unLock();

            TimerProc();
            return;
        } else {
            m_Lock.unLock();

            ResetTimer();
        }

    }
    }
};

public boolean ResetTimer() {
    long lTime = Math.min(1000, m_lTime) * 1000;
    if (lTime <= 0)
        lTime = 1;



    long testTime=0;
    while(testTime<=lTime)
    {
        if(testTime>=lTime)
        {
            if(!mUpdateTimeTaskThread.isAlive())
            mUpdateTimeTaskThread.start();
        }
        else
         testTime++;
    }
    return iTmID;
}

public boolean KillTimer() {
    m_Lock.getLock();
    if (iTmID) {
        iTmID = false;



        mUpdateTimeTaskThread.stop();
    }

    m_Lock.unLock();
    return true;
}

public boolean IsActive() {
    System.out.println("IsActive() in DSTimer iTmID"+iTmID);
    return (iTmID);
}

}

ハンドラーの代わりにスレッドを使用してJavaに変換しようとしました。しかし、illegalStateExceptionThreadエラーが発生します。だから私に解決策を教えてください。

編集:コメントからのスタックトレース:

java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at com.interplay.viewer.common.DSTimer.SetTimer(DSTimer.java:93) 
at com.interplay.viewer.signage.DisplayManager.ResetDisplay(DisplayManager.java:84)
at com.interplay.viewer.signage.ScheduleManager.StopCurSchedule(ScheduleManager.jav‌​a:108)
at com.interplay.viewer.signage.ScheduleManager.PlaySchedule(ScheduleManager.java:8‌​3) 
at com.interplay.viewer.ViewerMain$3.run(ViewerMain.java:526)
4

0 に答える 0