One of actions in Struts2 is using below code:
java.util.concurrent.ExecutorService myservice = Executors.newSingleThreadExecutor();
myservice.execute(new myTask(user, "add"));
Here, myTask is inner class which implements Runnable interface.
Can I invoke the above code from other action class as well by passing parameter a shown below:
java.util.concurrent.ExecutorService myservice = Executors.newSingleThreadExecutor();
myservice.execute(new myTask(user, "delete"));
In Run method, I’ll check the action & if its add, perform some activity, if its update, perform other activity….
Also, from another 3rd action class, can I invoke the same above thread by passing another action, say “update”???
class myTask implements Runnable{
private User user = null;
private String action = null;
myTask(User user, String action){
this.user = user;
this.action = action;
}
public void run(){
if (action.equals("add")) {
performAdd(user);
} else if (action.equals("delete")) {
performDelete(user);
}
}
Thanks!
No –
Runnable.rundoesn’t take any parameters, so how would you expect the “delete” part to be provided to you?Just use the first form – create the
Runnableinstance with all the information it needs to know so that it can do its work. Alternatively, I would probably create three different implementations ofRunnable– one for add, one for delete, and one for update. If you want to do one of three things, why switch on data at execution time when you can use polymorphism?