I'm working on an application that simulated process of writing text message on mobile phone. On the canvas I first draw already typed message. If any and on button pressed I retrieve correct set of characters. Here I'm able to loop through them and highlight currently active/selected character. However I need to hide this set of characters lets say after 2 sec. I made two attempt to solve this problem but both do not work properly.
Timer
public void keyPressed(int key)
{
//Rest of the function that is not relevant to problem
charSet = true;
repaint();
if(nml.getThreadState()) // boolean check, if true execute
{
System.out.println("Cancel timer");
timeTask.cancel();
System.out.println("Start new timer");
tk = new TimeKeeping();
timeTask.schedule(tk, 2000);
System.out.println("New timer started");
}
else
{
System.out.println("Start new timer");
tk = new TimeKeeping();
timeTask.schedule(tk, 2000);
System.out.println("New timer started");
}
}
private class TimeKeeping extends TimerTask
{
public void run()
{
nml.setThreadState(true);
charSet = false;
selChar = 0;
charArr = null;
lastChar = 0;
nml.setThreadState(false);
repaint();
System.out.println("Timer expired");
}
}
In this case I'm hardly able to reach 4th character before all is reset to zero. It leaves an impression timeTask.cancel() was not executed at all and the first invoked Timer just expired and executed content of run() method.
Thread
public void keyPressed(int key)
{
//Rest of the code
charSet = true;
repaint();
System.out.println("Number of active threads before is statement = "+Thread.activeCount());
if(nml.getThreadState())
{
timeTask.interrupt();
try
{
timeTask.start();
}
catch(IllegalThreadStateException itse)
{
}
}
else
{
try
{
timeTask.start();
}
catch(IllegalThreadStateException itse)
{
}
}
}
private class TimeKeeping extends Thread
{
public void run()
{
System.out.println("Number of active threads before run = "+Thread.activeCount());
nml.setThreadState(true);
try
{
Thread.sleep(2000);
}
catch(InterruptedException ie)
{
}
catch(IllegalMonitorStateException imse)
{
}
finally
{
charSet = false;
selChar = 0;
charArr = null;
lastChar = 0;
nml.setThreadState(false);
repaint();
System.out.println("Timer expired");
notifyAll();
}
}
}
In this scenario I'm able to loop through all characters, however when sleep time should expire the character set does not hide as if repaint method is never called. Bellow is small example of messages that I receive during execution
Number of active threads before is statement = 2
Number of active threads after if = 3
Number of active threads before run = 3
Number of active threads before is statement = 2
Number of active threads after if = 3
Uncaught exception java/lang/IllegalMonitorStateException.
Timer expired
Number of active threads before is statement = 2
Number of active threads after if = 2
Number of active threads before is statement = 2
Number of active threads after if = 2
Execution completed.
I'm not sure how to handle that exception and where it should be caught :(
Does anyone see anything wrong with this code?
Please keep in mind this is Java Microedition...