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);
}
}
上面给出的是用android编写的代码。我需要将其更改为纯 Java。我已经尝试了下面的代码。但它显示了非法线程状态异常错误。
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。但是我收到了非法StateExceptionThread 错误。所以请告诉我一个解决方案。
编辑:来自评论的堆栈跟踪:
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.java:108)
at com.interplay.viewer.signage.ScheduleManager.PlaySchedule(ScheduleManager.java:83)
at com.interplay.viewer.ViewerMain$3.run(ViewerMain.java:526)