【发布时间】:2014-02-28 05:43:45
【问题描述】:
我有一个调用 MyService 类的执行方法的客户端类。该方法将依次调用一个 InsertDAO 类。这个 InsertDAO 具有带有实例变量的状态。这仅从 MyService 类的执行方法调用。不能直接调用 InsertDAO 类。
我没有创建任何线程,但我的应用服务器可能会在客户端类上创建线程。现在,我想了解这将如何影响 InsertDAO 类。
- 多个线程可以同时访问一个InsertDAO的对象吗? -- 是/否
- 当在 Client 类上产生线程时,MyService 类的相同实例将提供给 Client 上的所有线程。然后每个线程都应该调用 MyService 的“执行”方法。这意味着每个线程都有自己的 InsertDAO 实例(我在 MyService 类的执行方法中创建 InsertDAO 对象)。如果是这样,多个线程不能同时进入 InsertDAO。我的理解正确吗?
- 如何让多个线程进入 MyService.execute() -- 一些解释,如果这是真的。
- 如何让多个线程进入 InsertDAO 类? -- 一些解释,如果这是真的。
- 如何在不影响性能的情况下使其成为线程安全的?
专家,请分享您对此的看法。下面是我的代码。
//code starts here
public class Client{
public void performExecution(){
InvoiceVO createInvoiceVO = new InvoiceVO();
MyService service = new MyService();
createInvoiceVO = service.execute(createInvoiceVO);
//retrieve successful/failure information from createInvoiceVO
}
}
public class MyService{
public InvoiceVO execute(InvoiceVO createInvoiceVO){
InsertDAO insertDAO = new InsertDAO();
insertDAO.process(createInvoiceVO);
}
}
public class InsertDAO{
private List<LineItem> lineItemsList = new ArrayList<LineItem>();
private List<TaxVO> taxVOList = new ArrayList<TaxVO>();
private Connection connection = null;
public InvoiceVO process(InvoiceVO createInvoiceVO){
this.lineItemsList = createInvoiceVO.getLineItemsList();
this.taxVOList = createInvoiceVO.getTaxVOList();
connection = getConnection();
//insert tax vo objects
insertTaxVOObjects(taxVOList);
//insert line items
insertLineItems(this.lineItemsList);
//commit operation
//close connection
closeConnection();
}
private void insertTaxVOObjects(List<TaxVO> taxVOList){
//code to insert TaxVO objects
}
private void insertLineItems(List<LineItem> lineItemsList){
//code to insert LineItem objects
}
private void getConnection(){
//code to return connection
}
private void closeConnection(){
//code to close connection
}
}
【问题讨论】:
-
编辑引用对象
标签: java multithreading performance