我編寫了一個用於創建Record實例的內部類CreateRecord。稍後在調用create方法時將其作爲任務提交給執行程序。 我是否每次都要創建一個內部類來表示要執行的任務(例如deleteRecord,updateRecord)。 任何人都可以提出一個更好的方法。向執行者添加任務
ExecutorService exec=new ThreadPoolExecutor(corePoolSize,maxPoolSize,
keepAlive,TimeUnit.MINUTES,new LinkedBlockingQueue<Runnable>());
public void create(String str) throws RemoteException
{
exec.execute(new CreateRecord(str));
}
class CreateRecord implements Runnable
{
private String Id;
public CreateRecord(String Id)
{
this.Id=Id;
}
@Override
public void run()
{
System.out.println("Record creation in progress of: "+Id+
" by thread: "+Thread.currentThread().getName());
Record rec=new Record(Id);
map.put(Id, rec);
}
}
我不太喜歡[stringly typed](http://c2.com/cgi/wiki?StringlyTyped)[命令模式](https://en.wikipedia.org/wiki/Command_pattern )。 – Jeffrey
如果Enum是UPDATE,INSERT和DELETE並且使用switch語句,那麼操作應該是Enum類型。 – mdewitt