[阅读: 466] 2005-11-29 07:58:13
public myCanvas (Composite arg0, int arg1)
{
Timer t = new Timer();
MyTask task = new MyTask (this);
t.schedule(task, 600, 200);
}
I am not sure if it is OK if you define the timer as a local variable inside the constructor. When the constructor exits the timer will be destroyed. I guess the exception happened only after you executes c.redraw(), which means the timer object no longer available.
Another way of implementing your timer approach is:
/*
* Created on Jun 17, 2005
*
* Class to queue the reqeust and run the action.
*/
package com.bnpparibas.command.server;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.bnpparibas.command.action.Action;
/**
* @author Alex SUO
*
* This class is used to queue the request from the client and execute
* the action. The class provide synchronization and threading for
* the execution to achieve maximized performance and safety.
*/
public class ActionRunner implements Runnable {
/**
* Implement this method to provide threading execution for
* the actions. Each action will be put into a separate thread.
* @see java.lang.Runnable#run()
*/
public void run() {
while (true) {
if (queue.size() > 0) {
final Action action = (Action)queue.get(0);
queue.remove(0);
Thread runner = new Thread() {
public void run() {
runAction(action);
}
};
runner.start();
}
try {
//The 40 seconds sleep here is to prevent the
//server from being overloaded.
Thread.sleep(40000);
} catch (InterruptedException e) {
}
}
}
/**
* Run a single action in synchronized mode.
* @param action The action to be executed.
*/
protected void runAction(Action action) {
try {
action.execute();
} catch (Exception e) {
LOGGER.debug("Exception during execution " + action.getType() +
" on " + action.getParam("ACCOUNT"));
LOGGER.debug(e);
}
}
}
The main method in another class:
/**
* The entrance of the application.
* @param args The arguments of the program.
*/
public static void main(String[] args) {
try {
Processor p = new Processor(args[0]);
Thread t = new Thread(p);
t.start();
Thread e = new Thread(p.runner);
e.start();
System.out.println("Server started.");
t.join();
} catch (Exception e) {
System.out.println("Error in starting processor.");
e.printStackTrace();
System.exit(-1);
}
System.out.println("Server quit.");
}